創(chuàng)建tarBar后 需要添加多個(gè)navigationController或者viewController
如果挨個(gè)創(chuàng)建 會(huì)覺得很不方便
使用NSClassFromString(@"" )
字符串轉(zhuǎn)化為類名的方法 可以便捷創(chuàng)建
//創(chuàng)建tabBar
tabBar = [[UITabBarController alloc]init];
// VC/NC 的title 存放在一個(gè)array
NSArray *vcName = @[@"chat", @"friend", @"show", @"about"];
for (int i = 0; i < 4; i ++) {
NSString *classVName = [vcName[i] stringByAppendingString:@"ViewController"];
字符串轉(zhuǎn)化為類名 創(chuàng)建實(shí)例對(duì)象 NSClassFromString(字符串)
UIViewController *vc = [[NSClassFromString(classVName) alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[vc.navigationItem setTitle:vcName [i]]; nc.tabBarItem = [[UITabBarItem alloc]initWithTitle:vcName[i] image:nil selectedImage:nil]; [_tabBar addChildViewController:nc];
對(duì)于VC和NC的命名,目前我還沒有找到將字符串作為實(shí)例對(duì)象的名字的方法,所有創(chuàng)建的多個(gè)VC/NC名字都只能 UIViewController *vc
或是UINavigationController *nc