Well, I reproduced your issue, and solved it by moving the switching logic from -viewDidLoad to -viewDidAppear:. So basically, change:
- (void)viewDidLoad {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}
to:
- (void)viewDidAppear:(BOOL)animated {
// Other code...
if(x){
[self.tabBarController setSelectedIndex:1];
}
}
Now, as to why this is the case, I can only guess, without more digging, that it has to do with the order things are initialized. It is possible that your view controller's viewDidLoad is being called before the parent tab bar controller has finished its own initialization. Holding off until your view has actually appeared ensures that everything is loaded and in a consistent state.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…