0

我正在尝试确定用户选择了哪个选项卡。我从几个关于 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");
    }
}
4

2 回答 2

2

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

于 2012-05-15T20:51:02.120 回答
1

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

我还建议在您的委托方法中的 if 之外放置一个日志,以确认它确实被调用了。

于 2012-05-15T20:52:49.080 回答