0

I want to program an app that creates ViewControllers itself. Is it possible?

Right now I'm doing a tabbar that gets a random number(n) from a website and creates n tabs. When I run the app it's all ok but when I tap on one tab it fails without showing an error. How can I make it?

Here's is the simple code I'm using where pages is an array with the number of tabs that I want:

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];

I don't know if it's possible or how I can do it, any help will be welcome.

Thanks!!!

1 Answer 1

1

Yes very much possible,

The issue your having is the way you are adding the tabBarController "to the view". I was able to replicate your crashing bug and like you said no helpful warnings are given.

This is how you are doing it (my example is in didFinishLaunching in the appDelegate)

THE INCORRECT WAY

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;

The correct way would be to set the tabBarController as the windows rootViewController rather then adding the tabBarControllers view as a subview to your other view.

THE CORRECT WAY (also in didFinishLaunching in the appDelegate)

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;

Hope this sets you off on the right track. The take away message here is you should not being trying to add viewControllers views as subviews to other viewControllers views.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It solves the crash! Now my view is all black...I'm going to solve it! Lots of thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.