iOS选中标签

时间:2012-05-15 20:47:38

标签: ios uiviewcontroller tabs

我正在尝试确定用户选择了哪个标签。我在iOS标签栏上的几个教程中将它融合在一起。在我的appDelegate中,我有这段代码:

- (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");
    }
}

2 个答案:

答案 0 :(得分:2)

在这种情况下,您的app委托应该是tabBarController的委托。

答案 1 :(得分:1)

您只需添加self.tabController.delegate = self,并确保您的AppDelegate符合UITabBarControllerDelegate协议。

我还建议在委托方法中将if放在if之外,以确认它实际上已被调用。