将视图控制器添加到阵列

时间:2012-06-29 20:01:25

标签: iphone

如何将这四个视图控制器添加到数组

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;

得到黄色警告实例方法setSubViewController未找到返回类型默认为id

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

要将视图控制器添加到数组,不需要for循环。删除循环,然后添加:

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不是有效的方法。但是,您可以使用addChildViewController添加子视图控制器。你可以遍历你的数组,并调用[container addChildViewController:x #ViewController]; 类似的东西:

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

注意:我没有测试过这段代码。如果您有任何问题,请告诉我。

答案 1 :(得分:1)

设置此

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

ContainerViewController的.h中这将帮助你摆脱警告,但我不确定你在逻辑上做什么,我建议你在分配之前在循环中释放你的子视图控制器再次...