iOS 选定的选项卡 [英] iOS selected tab
问题描述
我正在尝试确定用户选择了哪个选项卡.我从 iOS 标签栏上的几个教程中将它们融合在一起.在我的 appDelegate 我有这个代码:
I'm trying to determine which tab has been selected by the user. I melded this together from a couple of tutorials on iOS tab bars. In my appDelegate I have this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//We need to implement the view controllers within the tab controller and make the tab controller the root controller of our app - note we are only using view 1-3 at first.
FirstViewController *fistView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
FourthViewController *fourthView = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
NSArray *viewControllersArray = [[NSArray alloc] initWithObjects:fistView, secondView, thirdView, fourthView, nil];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllersArray animated:YES];
self.window.rootViewController = self.tabController;
//end custom code
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
viewControllerArray 是我的 tabController 的委托吗?
Is viewControllerArray the delegate for my tabController?
当我将此代码放在页面上时,什么也没有发生:
When I place this code on the page nothing happens:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
NSLog(@"ok");
}
}
推荐答案
您可以简单地添加 self.tabController.delegate = self
并确保您的 AppDelegate 符合 UITabBarControllerDelegate代码>协议.
You can simply add self.tabController.delegate = self
and make sure that your AppDelegate conforms to the UITabBarControllerDelegate
protocol.
我还建议在您的委托方法中的 if 之外放置一个日志,以确认它确实被调用了.
I also suggest placing a log outside the if in your delegate method, to confirm that it is actually called.
这篇关于iOS 选定的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!