views:

210

answers:

1

I have an imageView and after i add it to the screen I release it. Before that I adds a button onto it with this code.

   button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
   button.frame = CGRectMake(128.00, 128.00, 23.00, 40.00);
   [button setBackgroundImage:[UIImage imageNamed:@"move.png"] forState:UIControlStateNormal];
   [button addTarget:self action:@selector(showAddress:) forControlEvents:UIControlEventTouchUpInside];
   [imageView addSubview:button];

            - (void) showAddress:(id)sender
            {
                NSLog(@"Working");
            }

The button appears but nothing happens when I click on it to call a functon. Not even the pressing-unpressing. Is this becoz I am releaseing the imageView?

+1  A: 

Edited:Check if userInteractionEnabled property of your UIImageView is set to YES - it is set to NO by default and that's why UIImageView (and its subviews) does not respond to touch events. I've just set it to YES in my code and it works fine.

One more note - when you add your button to another view it takes the ownership of the button and as you retained it in your code you should also release it. (Or just not retain in this case)

Vladimir
since i have subclassed and written the touch functions when I click on the button, it calls the touchesBegan and touchesEnded. But the button is just like an image now which is having no effect. Or should I change the addTarget to imageView rather then self?
wolverine
See edited answer
Vladimir
Ok boss, it worked. I removed imageView.userInteractionEnabled = YES from - viewDidLoad and placed it in the code just before I add the imageViews into the scrollView. Thanks, u r a real lifesaver,:)
wolverine