Reputation: 1620
I am trying to call the UIBarButtonItem
when the back button of that controller is pressed.This is my code.I have put the initialization in viewDidLoad
and viewWillAppear
but the method is not being called.
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];
self.navigationItem.backBarButtonItem = exampleButton;
-(void)backToViewController:(UIBarButtonItem*)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Upvotes: 1
Views: 611
Reputation: 3389
IT will not add backBarButtonItem
with your line as :
self.navigationItem.backBarButtonItem = exampleButton;
You have to Provide leftbarbutton
as
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:self action:(@selector(backToViewController:))];
// As you are going with " ", there is no text, that means you have to click at left side randomly, to get affect.
self.navigationItem.leftBarButtonItem = exampleButton;
Update 1
You can provide Custom button with Image created by you, as you want,
UIButton *barCutomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[barCutomBtn setImage:[UIImage imageNamed:@"backImage.png"] forState:UIControlStateNormal];
[barCutomBtn addTarget:self action:@selector(backToViewController:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *exampleButton = [[UIBarButtonItem alloc] initWithCustomView:barCutomBtn];
self.navigationItem.leftBarButtonItem = exampleButton;
Upvotes: 1