Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 501
Moussa Avatar asked Jul 28 '14 09:07

Moussa


2 Answers

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");
}
like image 131
Kathiravan G Avatar answered Oct 21 '22 12:10

Kathiravan G


Swift

    let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.Action, target: self, action: Selector("userDidTapShare"))

    self.navigationItem.rightBarButtonItem = shareBar

     func userDidTapShare() {
          //Implementation goes here ...
    }
like image 38
Bobj-C Avatar answered Oct 21 '22 12:10

Bobj-C