2013-01-20 27 views
1

我無法更改我的UITabBar的索引。我試圖從UITabBarController子類中更改它。這就是我想:UITabBar無法更改所選索引

[self.tabBarController setSelectedIndex:1]; 

這是我的.h是什麼樣子

#import <UIKit/UIKit.h> 

@interface CustomTab : UITabBarController <UITabBarControllerDelegate>{ 
    IBOutlet CustomTab *bar; 
} 

@end 

我想設置與IBAction爲索引,該方法被調用,但指數不會改變。有任何想法嗎?

+0

這個工作從其他viewcontrollers,它有來自自身的UITabBarController被稱爲辦? – davis

+0

IBAction是否在CustomTab中實現?因爲如果沒有,你需要確保self.tabBarController指向你的CustomTab。 –

+0

它是在CustomTab中實現的,我設置了一個按鈕並添加如下所示的動作:'[button addTarget:nil action:@selector(change)forControlEvents:UIControlEventTouchUpInside];' – davis

回答

2

因爲你是從CustomTab類本身內部調用

[self.tabBarController setSelectedIndex:1]; 

。您需要

[self setSelectedIndex:1]; 
+0

哈哈。那個人也讓我有一段時間了..容易做到! –

0

來取代它的迅速

override func viewWillAppear(_ animated: Bool) { 
    self .selectedIndex = 3 // desired index 
}