Ios 以编程/自动方式创建ViewController

Ios 以编程/自动方式创建ViewController,ios,ios7,uiviewcontroller,uitabbarcontroller,Ios,Ios7,Uiviewcontroller,Uitabbarcontroller,我想编写一个应用程序来创建ViewController本身。可能吗 现在我正在做一个tabbar,它从一个网站获取一个随机数(n),并创建n个选项卡。当我运行应用程序时,一切正常,但当我点击一个选项卡时,它会失败而不显示错误。我怎样才能做到 下面是我使用的简单代码,其中pages是一个数组,包含我想要的选项卡数: NSMutableArray* controllers = [[NSMutableArray alloc] init]; for (int i=0; i<[pages

我想编写一个应用程序来创建ViewController本身。可能吗

现在我正在做一个tabbar,它从一个网站获取一个随机数(n),并创建n个选项卡。当我运行应用程序时,一切正常,但当我点击一个选项卡时,它会失败而不显示错误。我怎样才能做到

下面是我使用的简单代码,其中pages是一个数组,包含我想要的选项卡数:

NSMutableArray* controllers = [[NSMutableArray alloc] init];

    for (int i=0; i<[pages count]; i++) {

        UIViewController * vc1 = [[UIViewController alloc] init];

        vc1.title = [[pages objectAtIndex:i] objectForKey:@"title"];
        [controllers addObject:vc1];
    }

    tabBarController.viewControllers = controllers;

    [_window addSubview:tabBarController.view];
NSMutableArray*控制器=[[NSMutableArray alloc]init];

对于(int i=0;iYes)而言,非常可能

你的问题是你将tabBarController添加到视图的方式。我能够复制你的崩溃错误,就像你说的,没有给出有用的警告

您就是这样做的(我的示例是在appDelegate中的didFinishLaunching)

错误的方法

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

UIViewController *vc = [[UIViewController alloc] init];
[vc.view setFrame:self.window.frame];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSMutableArray* controllers = [[NSMutableArray alloc] init];

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

    UIViewController * vc1 = [[UIViewController alloc] init];

    vc1.title = [NSString stringWithFormat:@"%d", i];
    [controllers addObject:vc1];
}

[tabBarController setViewControllers:controllers];

self.window.rootViewController = vc;

[vc.view addSubview:tabBarController.view];
return YES;
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
UIViewController*vc=[[UIViewController alloc]init];
[vc.view setFrame:self.window.frame];
UITabBarController*tabBarController=[[UITabBarController alloc]init];
NSMutableArray*控制器=[[NSMutableArray alloc]init];

for(int i=0;iThanks!它解决了崩溃问题!现在我的视图全黑了…我要解决它!非常感谢!
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSMutableArray* controllers = [[NSMutableArray alloc] init];

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

    UIViewController * vc1 = [[UIViewController alloc] init];

    vc1.title = [NSString stringWithFormat:@"%d", i];
    [controllers addObject:vc1];
}

[tabBarController setViewControllers:controllers];

self.window.rootViewController = tabBarController;
return YES;