Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

iphone - How to get title of UITabBarItem in the More section?

I have a UITabBarControllerDelegate method that determines the title of the UITabBarItem and does something accordingly. This works well for items in my UITabBar but when I click on the More button the rest of my UITabBarItems are in a UITableView. How can I determine the title in the More section?

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    if ([self.tabBarController.selectedViewController.title isEqualToString:@"All"]) {
        //do something
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whenever you select a view controller in your UITabBarController, the method you mention will be called, and most important, the view controller currently shown will be passed to you as parameter; you can then use the following code to find the class and title of the controller, including the "more" controller:

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);
}

In a quick test, just by adding a couple of controllers in Xcode, this is what you get in the console:

2011-03-28 09:13:21.795 TabTest[39015:207] controller class: UIViewController
2011-03-28 09:13:21.797 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:23.922 TabTest[39015:207] controller class: UITableViewController
2011-03-28 09:13:23.925 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:24.505 TabTest[39015:207] controller class: UITableViewController
2011-03-28 09:13:24.506 TabTest[39015:207] controller title: (null)
2011-03-28 09:13:24.945 TabTest[39015:207] controller class: UIMoreNavigationController
2011-03-28 09:13:24.945 TabTest[39015:207] controller title: More

On the other side, when you select a controller inside the "more" list, you won't be notified in your UITabBarControllerDelegate method (weird, IMHO). To help you get notifications when you select controllers in that list, you could do the following:

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{
    if (navigationController == self.tabBarController.moreNavigationController)
    {
        NSLog(@"more controller class: %@", NSStringFromClass([viewController class]));
        NSLog(@"more controller title: %@", viewController.title);
    }
}

Your class should also implement the UINavigationControllerDelegate protocol, of course.

This is the result of a sample run, using the above code and tapping a couple of times in the UITabBar and the "more" list:

2011-03-28 09:27:42.496 TabTest[39113:207] controller class: UIViewController
2011-03-28 09:27:42.498 TabTest[39113:207] controller title: (null)
2011-03-28 09:27:44.306 TabTest[39113:207] controller class: UIMoreNavigationController
2011-03-28 09:27:44.307 TabTest[39113:207] controller title: More
2011-03-28 09:27:44.310 TabTest[39113:207] more controller class: UIMoreListController
2011-03-28 09:27:44.311 TabTest[39113:207] more controller title: More
2011-03-28 09:27:45.632 TabTest[39113:207] more controller class: SecondViewController
2011-03-28 09:27:45.634 TabTest[39113:207] more controller title: (null)
2011-03-28 09:27:47.156 TabTest[39113:207] more controller class: UIMoreListController
2011-03-28 09:27:47.156 TabTest[39113:207] more controller title: More
2011-03-28 09:27:48.581 TabTest[39113:207] controller class: UITableViewController
2011-03-28 09:27:48.582 TabTest[39113:207] controller title: (null)

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...