iosobjective-cxcodeselectoruinavigationitem

How to add an action to share button in navigation bar with Xcode


I am trying to add an action to my share button in navigation bar but I don't know how and where to define my "shareAction" method. To add share button, I have the following code in viewWillAppear :

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;

Solution

  • in your implementation.m file

    - (void) viewWillAppear 
    {
        [super viewWillAppear:animated];
        UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                        target:self
                                        action:@selector(shareAction:)];
        self.navigationItem.rightBarButtonItem = shareButton;
    }
    
    -(void)shareAction:(id)sender
    {
        NSLog(@"share action");
    }