2014-09-29 42 views
1

我有一個包含UITabBarController的應用程序。只有滿足條件時才應該輸入最後一個選項卡。目前,我正在使用功能viewDidAppear檢查此情況。但是,是否也可以在標籤打開並顯示之前進行每次檢查?iOS TabBarController:在進入新標籤之前運行檢查

根據第一反應我加入這兩個文件:

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 
+0

如何更改所選標籤的色​​調顏色? – user1324887 2016-08-01 02:17:42

回答

3

在MainTabBarController.m文件,在viewDidLoad中添加委託

@interface MainTabBarController()<UITabBarControllerDelegate> 

然後,添加

self.delegate = self; 

然後添加方法:

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

是它也可以每天做這種檢查時間前標籤是 打開並顯示?

你需要重寫UITabBarControllerdelegate方法是:

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

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

} 
+0

好吧,我給Tab Bar Controller分配了一個新類,並實現了你所說的方法。但它不起作用。我是否也需要更改頭文件? – Pascal 2014-09-29 12:26:13

相關問題