I tried to create a right bar button, but the action doesn’t get triggered when the button is touched. any ideas?
button = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:image]];
button.action = @selector(myaction);
button.target = self;
self.navigationItem.rightBarButtonItem = button;
[button release];
Unfortunately, you can’t trigger actions on
UIBarButtonItems that are created with custom views. The only way for this to work is if your custom view is actually aUIControlor something else that responds to touch events.If you need to support pre-3.2, the best way to deal with this is to create a button instead of an image view, and set the action on the button. If you can get away with supporting 3.2+, you can just add a
UIGestureRecognizerto your view (BTW: in your code, your image view is leaking, see below for proper use):Now it will work as expected without having to fob a
UIButtonin there that you might not want…