【问题标题】:Add view controllers to array将视图控制器添加到数组
【发布时间】:2012-06-29 20:01:24
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    要将视图控制器添加到数组中,不需要 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];
    }
    

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

    【讨论】:

      【解决方案2】:

      设置这个

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-03
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多