2013-01-20 62 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 
}