Get title of selected tab in UITabBarController

ⅰ亾dé卋堺 提交于 2019-12-24 17:17:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!