Reputation: 1093
Fairly new to the coding community and took over an app from my developer. I want to implement a Back button on the navigation bar of one of the views in my app. If you guys could point me in the right direction, that'd be greatly appreciated.
Thanks very much!
Upvotes: 0
Views: 598
Reputation: 415
in some scenarios, where you are not using an UINavigationController, you can create a UINavigationBar yourself.
self.navBar = [UINavigationBar new];
self.navBar.contentMode = UIViewContentModeTopLeft;
self.navBar.delegate = self;
self.navBar.frame = CGRectMake(0, 30, mainWindow.frame.size.width,40);
UINavigationItem *backButton = [[UINavigationItem alloc] initWithTitle:@"Back"];
[self.navBar setItems:[NSArray arrayWithObjects:backButton,[[UINavigationItem alloc] initWithTitle:@"Current View"], nil]];
[self.view addSubview: self.navBar];
Your controller should then implement the UINavigationBarDelegate protocol, so that you can respond to
-(BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
where you can go back to your previous view. There is mostly no use-case for this. You'll probably really want to work with UINavigationControllers and the push- and popViewController methods. I hope this helped anyway. kind regards.
Upvotes: 3
Reputation: 95579
The back button is added automatically when you use [UINavigationController -pushViewController]. Basically, instead of having a regular view controller, you need a navigation controller that is initialized with the default view, and when you want to show the other view, you need to push it on the navigation controller. By pushing the new view controller, it automatically adds the back button. The title of the back button is typically the title of the view controller, but you can change it with navigationBar.backItem.title.
Upvotes: 1