2012-05-15 77 views
0

我在尝试确定哪个选项卡已被用户选中。我从iOS标签栏上的一些教程中融合了这些内容。在我的appDelegate我有这个代码:iOS选项卡

- (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吗?

当我把这个代码的页面没有任何反应:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    if (tabBarController.selectedIndex == 0) { 
     NSLog(@"ok"); 
    } 
} 

回答

1

您可以简单地添加self.tabController.delegate = self并确保您的AppDelegate符合UITabBarControllerDelegate协议。

我也建议在你的委托方法中放置一个if以外的if来确认它实际上是被调用的。

+0

谢谢,这工作。我仍然对此很模糊,是否与通知中心类似,您可以将事件返回给应用程序控制器? – PruitIgoe

2

在这种情况下,您的应用程序代理应该是tabBarController委托。

相关问题