Determining which UIViewController subclass that a UITabBar Item belongs to
I have a UITabBarController instansiated in my appDelegate, but I need a way to know when the user presses the different tab bar items (buttons on the tab bar).
-UITabBarDelegate protocol to the rescue (with the required didSelectViewController-method)!
With everything wired up in Interface Builder, how do I开发者_开发知识库 obtain a reference to the actual UIViewController subclass instance that corresponds to this tab bar item that was pressed?
I need this reference because I need to call a method in one of my UIViewControllers subclasses every time that tab bar item is pressed.
Any suggestions?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
Possibly I am not understanding your question correctly, but it seems that what you're asking for is simply the "viewController" parameter which is passed into the method call you've mentioned
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
UITabBarController also has a property to get the same info
@property(nonatomic, assign) UIViewController *selectedViewController
精彩评论