Adding action and target to a custom Navigation Ba

2019-02-13 18:34发布

问题:

I am trying to customize the Navigation Bar by adding a custom button to the rightBarButtonItem. Creating one is 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 that now I can't set the target or action properties. The following code does nothing:

myButtonItem.target = myTarget;
myButtonItem.action = @selector(myButtonAction);

I found that the reason it's not working is because I initialized the UIBarButtonItem with -initWithCustomView. Apples documentation says:

The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.

I'm a little confused by what exactly apple means by that. Am I supposed to create a custom control or something? I haven't really done that yet and I'm not sure where to start.

Does anyone have any suggestions on how to do this or an alternative way to set up a custom Navigation Bar Button?

ANSWERED----------------------------------------------------------

Just had to add this method to the UIButton:

[myButton addTarget:myTarget action:@selector(myButtonAction) forControlEvents:UIControlEventTouchUpInside];

回答1:

It's pretty straight forward, you need to add target for uibutton not for uibarbuttonitem.

To explain more what you want for uibarbuttonitem is respond touch up inside action method, as documentation says that you need to do that for uibutton as you are initializing your uibarbuttonitem with custom view in your case uibutton.



回答2:

Add an action and target to a UIButton like this:

[myButton addTarget:self
             action:@selector(handleMyButtonTap)
   forControlEvents:UIControlEventTouchUpInside];

Add an action and target to a UIBarButtonItem like this:

[myBarButtonItem setAction:@selector(handleBarButtonTap)];
[myBarButtonItem setTarget:aTarget];


回答3:

With the help of answers by hgwhittle and Mustafa I implemented and it works perfect here is the complete snip may help someone

        //Create ImageButton
        UIImage *shareBtnIcon = [UIImage imageNamed:@"dummy_share_icon_22"];

        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareButton setFrame:CGRectMake(0, 0, shareBtnIcon.size.width, shareBtnIcon.size.height)];
        [shareButton setBackgroundImage:shareBtnIcon forState:UIControlStateNormal];

        [shareButton addTarget:self action:@selector(shareButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        //Create BarButton using ImageButton
        UIBarButtonItem *shareBarBtn = [[UIBarButtonItem alloc] initWithCustomView:shareButton];

        //Add BarButton to NavigationBar
        self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:shareBarBtn, nil];

//method for share butto click
-(void) shareButtonClicked:(UIButton*)sender
{
    NSLog(@"you clicked on share button %ld");
}