ios - selector is not called for navigationitem barbutton -
after set right bar navigation button custom view, selector never called when button pressed. here code:
uiimageview *navview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"notification_alert.png"]]; navview.frame = cgrectmake(0, 0, 40, 40); self.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview:navview]; [self.navigationitem.rightbarbuttonitem settarget:self]; [self.navigationitem.rightbarbuttonitem setaction:@selector(btnclick:)];
the button appears correctly selector never called. appreciated!
-(ibaction)btnclick:(id)sender { nslog(@"nav button clicked"); }
from documentation init(customview:)
:
the bar button item created method not call action method of target in response user interactions. instead, bar button item expects specified custom view handle user interactions , provide appropriate response.
see uibarbuttonitem class reference more information.
a workaround use uibutton
custom background image custom view, instead of uiimageview
. then, add target , action button.
uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; button.frame = cgrectmake(0, 0, 40, 40); [button setbackgroundimage:[uiimage imagenamed:@"background.png"] forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(btnclick:) forcontrolevents:uicontroleventtouchupinside]; uibarbuttonitem *barbuttontem = [[uibarbuttonitem alloc] initwithcustomview:button];
Comments
Post a Comment