UITabBar can't change selected index

独自空忆成欢 提交于 2020-01-05 07:22:08

问题


I'm having trouble changing the index of my UITabBar. I am attempting to change it from the UITabBarController subclass. This is what I'm trying:

[self.tabBarController setSelectedIndex:1];

This is what my .h looks like

#import <UIKit/UIKit.h>

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

@end

I am trying to set the index with an IBAction, the method is being called, but the index doesn't change. Any ideas?


回答1:


Because you are calling

[self.tabBarController setSelectedIndex:1];

from inside the CustomTab class itself. You need to replace it with

[self setSelectedIndex:1];



回答2:


for swift

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


来源:https://stackoverflow.com/questions/14429140/uitabbar-cant-change-selected-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!