-(void)viewDidAppear:(BOOL)animated
{
UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.backBarButtonItem = navigationBarbackButton;
}
-(void)backToRootView:(UIBarButtonItem *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Asked
Active
Viewed 1,445 times
1
-
1Please describe the problem in more detail, just posting the code is insufficient – ZeMoon Dec 11 '14 at 06:31
-
1Use `self.navigationItem.leftBarButtonItem` . – Bhumeshwer katre Dec 11 '14 at 06:31
-
http://stackoverflow.com/questions/2848055/add-button-to-navigationbar-programatically – the1pawan Dec 11 '14 at 06:31
-
Check this **http://stackoverflow.com/questions/18824186/trying-to-handle-back-navigation-button-action-in-ios/18824282#18824282** – Kumar KL Dec 11 '14 at 06:43
-
try use self.navigationItem.leftBarButtonItem = navigationBarbackButton; – larva Dec 11 '14 at 06:46
1 Answers
0
Here contradiction is that you are setting action event to backBarButtonItem
and also set it to hidden
. So it is not visible right now.
Remove below line from viewDidAppear()
method:
self.navigationItem.hidesBackButton = YES;
Also it is good programming to call super
class method for view life cycle methods.
So updated method will be:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
self.navigationItem.backBarButtonItem = navigationBarbackButton;
}
Edit:
backBarButtonItem
has default action to pop controller, you don't need to add an action event for that. I'm not sure about your concern to use back button but if you are trying customise left button item then must follow this code:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIBarButtonItem *navigationBarbackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRootView:)];
self.navigationItem.leftBarButtonItem = navigationBarbackButton;
}
-(void)backToRootView:(UIBarButtonItem *)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

Kampai
- 22,848
- 21
- 95
- 95
-
backBarButtonItem action can't be change. To use custom action or look must have to use leftBarButtonItem instead of backBatButtonItem – Bhumeshwer katre Dec 11 '14 at 06:50
-
Yes. you are right that's why I haven't include action method. But if OP's concern is with just to change left bar button item then let me update my answer. – Kampai Dec 11 '14 at 06:56