问题
I have set the title for all five of my tabs in my AppDelegate file. I would like to access that title in another view within my application. I have the code below to output to the log window the selected index of that selected tab but what I'd really like to get is the title for that tab. I did have a look through the UITabBarController Class Reference and I didn't see anything that would allow me to do this.
What I am trying to avoid is some sort of switch or if...else statement where I hardcode values I have already manually set in another file.
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"The currently selected tab has an index of: %d", appDelegate.tabBarController.selectedIndex);
}
This code works as intended. It would just be ideal to see the title.
回答1:
UIViewController* vc = appDelegate.tabBarController.selectedViewController;
NSString* tabTitle = vc.tabBarItem.title;
If the code is in the selected view controller it's even easier:
NSString* tabTitle = self.tabBarItem.title;
来源:https://stackoverflow.com/questions/12182631/get-title-of-selected-tab-in-uitabbarcontroller