0

How can i add these four view controllers to array

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];    
ContainerViewController  *container= [[[ContainerViewController alloc]init]autorelease];
self.window.rootViewController = container;  
NSMutableArray *controllers = [NSMutableArray array];
for (int i=0; i<23; i++)
{
    First *first = [[First alloc] init];
    Second *second = [[Second alloc] init];
    Third *third = [[Third alloc] init];
    Fourth *fourth = [[Fourth alloc] init];

    [controllers addObject:first];
    [controllers addObject:second];
    [controllers addObject:third];
    [controllers addObject:fourth];
    }

 [container setSubViewControllers:controllers];
[window makeKeyAndVisible];
return YES;

getting yellow warning that instance method setSubViewController not found return type defaults to id

Thanks for help.

2 Answers 2

2

To add the view controllers to the array, there's no need to have a for loop. Remove the loop, and add:

First *first = [[First alloc] init];
    Second *second = [[Second alloc] init];
    Third *third = [[Third alloc] init];
    Fourth *fourth = [[Fourth alloc] init];

    [controllers addObject:first];
    [controllers addObject:second];
    [controllers addObject:third];
    [controllers addObject:fourth];

For the view controllers in container: setSubViewControllers is not a valid method. However, you can add a child view controller with addChildViewController. You could loop through your array, and call [container addChildViewController:x#ViewController]; Something like:

for (id thisViewController in controllers) {
thisViewController = (UIViewController *)thisViewController;
[container addChildViewController:thisViewController];
}

Note: I haven't tested this code. Let me know if you have any problems.

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

Comments

1

set this

- (void)setSubViewControllers:(NSArray *)subViewControllers;

in the .h of ContainerViewController this will help you get rid of the warning but I am not sure what you are doing logically and also I would advice you to release your sub view controllers in the loop before allocating them again...

Comments

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.