How to get the text in a backBarButtonItem?

2019-04-17 17:09发布

问题:

does anyone knows how to get the text content of a backBarButtonItem when using a UINavigationController ? It's the name of the previous view but I'd like to get it in the next view.

Thanks!

回答1:

Here's one way to get the title of the previous viewController in the navigation controller viewController stack:

((UIViewController*)[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2]).title


回答2:

You need to create a UIBarButtonItem using:

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

Then you attach the target to your method and you can pop the controller in your method.



回答3:

It's property of UIBarItem: backBarButtonItem.title



回答4:

UIViewController* backController = (UIViewController*)[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count] - 2];
NSString* backTitle = backController.navigationItem.title;