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 に答える