ios - Adding action and target to a custom Navigation BarButtonItem? -
i trying customize navigation bar adding custom button rightbarbuttonitem. creating 1 pretty straight forward.
uiimage *myimage = [uiimage imagenamed:@"menu-icon.png"]; uibutton *mybutton = [uibutton buttonwithtype:uibuttontypecustom]; [mybutton setframe:cgrectmake(0, 0, myimage.size.width, myimage.size.height)]; [mybutton setbackgroundimage:myimage forstate:uicontrolstatenormal]; uibarbuttonitem *mybuttonitem = [[uibarbuttonitem alloc] initwithcustomview:mybutton]; [navitem setleftbarbuttonitem:mybuttonitem animated:yes];
looks good, except can't set target or action properties. following code nothing:
mybuttonitem.target = mytarget; mybuttonitem.action = @selector(mybuttonaction);
i found reason it's not working because initialized uibarbuttonitem -initwithcustomview. apples documentation says:
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.
i'm little confused apple means that. supposed create custom control or something? haven't done yet , i'm not sure start.
does have suggestions on how or alternative way set custom navigation bar button?
answered----------------------------------------------------------
just had add method uibutton:
[mybutton addtarget:mytarget action:@selector(mybuttonaction) forcontrolevents:uicontroleventtouchupinside];
it's pretty straight forward, need add target uibutton not uibarbuttonitem.
to explain more want uibarbuttonitem respond touch inside action method, documentation says need uibutton initializing uibarbuttonitem custom view in case uibutton.
Comments
Post a Comment