Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.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
Iphone 切换选项卡栏时检测选定的选项卡栏_Iphone_Ios_Objective C - Fatal编程技术网

Iphone 切换选项卡栏时检测选定的选项卡栏

Iphone 切换选项卡栏时检测选定的选项卡栏,iphone,ios,objective-c,Iphone,Ios,Objective C,我有三个选项卡栏应用程序,我的选项卡是TAB1、TAB2、TAB3。我在TAB1视图控制器中编写了以下代码,以检测用户按下了哪个选项卡 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { NSLog(@"tab selected: %@", item.title); } 但这名代表从未接到过电话 我已经在appdelegate.m中设置了我的选项卡 - (void)setupTabBar

我有三个选项卡栏应用程序,我的选项卡是TAB1、TAB2、TAB3。我在TAB1视图控制器中编写了以下代码,以检测用户按下了哪个选项卡

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    NSLog(@"tab selected: %@", item.title); 
} 
但这名代表从未接到过电话

我已经在appdelegate.m中设置了我的选项卡

- (void)setupTabBar 
{
    self.myWorkListViewController = [[MyWorkListViewController alloc] initWithNibName:@"MyWorkListViewController"
                                                                               bundle:nil];
    self.askHRViewController = [[AskHRViewController alloc] initWithNibName:@"AskHRViewController"
                                                                     bundle:nil];

    self.moreViewController = [[MoreViewController alloc] initWithNibName:@"MoreViewController"
                                                                           bundle:nil];

    self.bookLeaveViewController = [[BookLeaveViewController alloc] initWithNibName:@"BookLeaveViewController"
                                                                             bundle:nil];
    self.helpViewController = [[HelpViewController alloc] initWithNibName:@"HelpViewController"
                                                                   bundle:nil];

    // Create navigation controllers
    workListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.myWorkListViewController];

    askHRNavigationController = [[UINavigationController alloc] initWithRootViewController:self.askHRViewController];

    bookLeaveViewController = [[UINavigationController alloc] initWithRootViewController:self.bookLeaveViewController];

    moreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.moreViewController];

    helpNavigationController = [[UINavigationController alloc] initWithRootViewController:self.helpViewController];

    [self setTabBarImagesAndText];

    // Setup tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
    //Set TabBar background
    [self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
    [self.tabBarController setSelectedIndex:0];

}

您可以通过以下方式检测选定的选项卡栏项:- 与你的代码一样,你只需要添加这个行

// Setup tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
    //Set TabBar background
    [self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
    [self.tabBarController setSelectedIndex:0];
在.h文件中定义like

@interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
   //declare your Tabbar controller with @proparty 
}
现在把这个UITabbarController的委托

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}

您可以通过以下方式检测选定的选项卡栏项:- 与你的代码一样,你只需要添加这个行

// Setup tab bar controller
    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.delegate=self;
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:workListNavigationController, askHRNavigationController, bookLeaveViewController, helpNavigationController,moreNavigationController, nil]];
    //Set TabBar background
    [self.tabBarController.tabBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TabBar_iOS4_Background"]] atIndex:0];
    [self.tabBarController setSelectedIndex:0];
在.h文件中定义like

@interface yourViewcontroller : UIViewController<UITabBarControllerDelegate>
{
   //declare your Tabbar controller with @proparty 
}
现在把这个UITabbarController的委托

- (void)tabBarController:(UITabBarController *)tabBarController 
 didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

    if (viewController == tabBarController.moreNavigationController)
    {
        tabBarController.moreNavigationController.delegate = self;
    }
}
您必须使用代码(在ViewDidLoad等中)或在interface builder中“连接”代理。
看看这个解释如何连接textView委托的答案(基本相同):

您必须使用代码(在ViewDidLoad等中)或在interface builder中“连接”委托。

看看这个答案,它解释了如何连接textView委托(基本相同):

这个委托方法在TAB1viewcontroller中也没有被调用:(它可以工作,谢谢:):)但我很惊讶,我必须在每个控制器中设置委托。我们不能一次性进行此设置吗?请参阅此
didSelectViewController
它是
UITabbarController
的委托方法,因此,无论何时需要使用,都必须声明特定UITabbarController的委托,这就是为什么此委托方法也不会被调用的原因在TAB1viewcontroller中:(它可以工作,谢谢:):)但我很惊讶,我必须在每个控制器中设置委托。我们不能只进行一次设置吗?请参阅此
didSelectViewController
这是
UITabbarController
的委托方法,所以无论何时需要使用,都必须声明特定UITabbarController的委托,这就是原因