2012-04-08 100 views
1

我有選項卡式iOS應用程序。我需要知道哪個選項卡處於活動狀態,並檢測選項卡更改的時間。在故事板中,我有一個選項卡視圖控制器,當您單擊一個選項卡時更改視圖。我創建了一個類TabBarController和它的定義如下:iOS檢測標籤是否被更改

部首

@interface TabBarController : UITabBarController <UITabBarControllerDelegate> 

@end 

實施

#import "TabBarController.h" 

@implementation TabBarController 

// In the initialization section, set the delegate 
- (id) init 
{ 
    self = [super init]; 
    if (self) 
    { 
     self.delegate = self; 
    } 
    return self; 
} 

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

@end 

然而,我無法檢測到與上述代碼標籤的變化。你認爲這個問題是什麼?

我沒有將我的標籤頁視圖鏈接到任何網點,但是轉到其他視圖。這是問題嗎?那麼,我應該在哪裏連接我的插座?

回答

-3

解決,這是實現viewDidLoad中如下:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    NSLog(@"Tabs showed up!"); 
    self.delegate = self; 
} 
+0

爲什麼在接受O_o時如此低估? – Alfabravo 2017-06-12 18:22:03

3

您確定您的init方法正在被調用嗎?我不認爲initUITabBarController的指定初始化程序,從nib/storyboard加載控制器時可能不會調用。

如果是這樣的話,你可能會發現更容易設置委託在viewDidLoad因爲這將不考慮所謂的對象是如何初始化,否則請務必設置委託時-initWithNibName:bundle:-initWithCoder來實例化對象。

+0

感謝@Jonah。我通過把self.delegate = self解決了這個問題;到viewDidLoad中。 – mert 2012-04-08 19:13:13