iOS TabBarController:在输入新标签之前运行检查

时间:2014-09-29 11:44:25

标签: ios uitabbarcontroller

我有一个包含UITabBarController的应用程序。只有在满足条件时才能输入最后一个选项卡。目前我正在函数viewDidAppear检查这个条件。但是,每次打开和显示选项卡之前,是否也可以进行此检查?

根据第一个回复我添加了这两个文件:

MainTabBarController.h

#import <UIKit/UIKit.h>

@interface MainTabBarController : UITabBarController

@end

MainTabBarController.m

#import "MainTabBarController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];

    if(indexOfTab == 2)
    {
        NSLog(@"Is it working?");
    }

}

@end

2 个答案:

答案 0 :(得分:3)

在MainTabBarController.m文件中,添加一个委托

@interface MainTabBarController ()<UITabBarControllerDelegate>

然后在viewDidLoad中添加

self.delegate = self;

然后添加一个方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:       (UIViewController *)viewController
{
    //add your check here
    return YES;
}

答案 1 :(得分:0)

  

每次选项卡之前都可以进行此检查   打开并显示?

您需要覆盖UITabBarController delegate方法:

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
   NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];

   if(indexofTab == yourTabIndexToMetCondition){
      // Do your stuff here 
    }

}