Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS选定选项卡_Ios_Uiviewcontroller_Tabs - Fatal编程技术网

iOS选定选项卡

iOS选定选项卡,ios,uiviewcontroller,tabs,Ios,Uiviewcontroller,Tabs,我正在尝试确定用户选择了哪个选项卡。我从iOS选项卡栏上的两个教程中将其融合在一起。在我的appDelegate中,我有以下代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bou

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

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

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

您只需添加
self.tabController.delegate=self
,并确保您的应用程序代理符合
UITabbarController
协议


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

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


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

谢谢,这样做有效。我仍然不清楚,它是否类似于通知中心,您可以将事件返回到应用程序控制器?谢谢,这很有效。我仍然不清楚,它是否类似于通知中心,您可以将事件返回给应用程序控制器?