1.在用tabBarController管理控制器的时候,出现了下面的问题
对应的代码:
JingHuaController *jinghuaVC = [[JingHuaController alloc]init]; ZuixinViewController *zuiXinVC = [[ZuixinViewController alloc]init]; //FaTieController *faTieVC = [[FaTieController alloc]init]; AttentionController *attentionVC = [[AttentionController alloc]init]; WoViewController *woVC = [[WoViewController alloc]init];
//添加控制器,设置对应tabItem的图片
[self addChild:jinghuaVC image:kImage(@"tabBar_essence_icon") selectedImage:kImage(@"tabBar_essence_click_icon")];
[self addChild:zuiXinVC image:kImage(@"tabBar_new_icon") selectedImage:kImage(@"tabBar_new_click_icon")];
//[self addChild:faTieVC image:kImage(@"tabBar_publish_icon") selectedImage:kImage(@"tabBar_publish_click_icon")];
[self addChild:attentionVC image:kImage(@"tabBar_friendTrends_icon") selectedImage:kImage(@"tabBar_friendTrends_click_icon")];
[self addChild:woVC image:kImage(@"tabBar_me_icon") selectedImage:kImage(@"tabBar_me_click_icon")];
- (void)addChild:(UIViewController *)ViewController image:(UIImage *)image selectedImage:(UIImage *)selectedImage{
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:ViewController];
[self addChildViewController:nav];
ViewController.tabBarItem.image = image;
ViewController.tabBarItem.selectedImage = selectedImage;
}
//控制器中的代码
@implementation AttentionController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"关注"; //在控制器中设置文字,问题就出在这里。我们知道,控制器的view是懒加载,即需要显示的时候才会加载。虽然开始我通过addChildController方法把相应controller加入到tabBarController中,但是界面出来时,只会显示第一个控制器的view,所以其他几个controller页面并没有显示,所以没有加载。所以它们的viewDidLoad方法并没有调用,“self.title = @"关注"根本没有执行,tabBar上怎么会显示呢?
解决办法:在添加控制器的时候,就一一设置tabBarItem.title就可以啦!
}
@end