tarBar添加多个NC

2016-11-17  本文已影响23人  路这么长

创建tarBar后 需要添加多个navigationController或者viewController

如果挨个创建 会觉得很不方便
使用NSClassFromString(@"" )字符串转化为类名的方法 可以便捷创建
//创建tabBar
tabBar = [[UITabBarController alloc]init];
// VC/NC 的title 存放在一个array
    NSArray *vcName = @[@"chat", @"friend", @"show", @"about"];

    for (int i = 0; i < 4; i ++) {

        NSString *classVName = [vcName[i] stringByAppendingString:@"ViewController"];

 字符串转化为类名 创建实例对象 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];
    

对于VC和NC的命名,目前我还没有找到将字符串作为实例对象的名字的方法,所有创建的多个VC/NC名字都只能 UIViewController *vc或是UINavigationController *nc

有知道的大神麻烦告知🙏
上一篇 下一篇

猜你喜欢

热点阅读