newsted
newsted

Reputation: 3

How can I set to a default UIBarButtonItem a custom action?

I have the following storyboard scenario linked with a navigationController:

Can I set the UIBarButtonItem action for always go back to Home storyboard and not to Sixth storyboard?

Any help will be appreciated.

Thank you!

Upvotes: 0

Views: 269

Answers (2)

carlos21
carlos21

Reputation: 485

First create the UIBarButtonItem:

UIBarButtonItem* cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Go Home" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTapped)];

self.navigationItem.leftBarButtonItem = cancelButtonItem;

If you want to go to the first controller of the navigation:

- (void)cancelTapped {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

Upvotes: 0

Balaj Narasimhan
Balaj Narasimhan

Reputation: 308

The Navigation controller mechanism will create automatic back button.

You may try using custom bar button and code for moving from one controller to another controller.

custom bar button will help you solving the problem. The code for moving from one page to other is as follows

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)

Upvotes: 1

Related Questions