-1

我想加载一个 UIView 作为第一页,然后加载一些 UIImageViews。UIImageViews 加载正常,但第一个 UIView 显示为空白。我在故事板中设计了 UIView。它有一些 UIButtons 和一些其他控件。我究竟做错了什么?

我从默认的 pageController 模板创建了项目。

模型控制器.m

- (id)init
{
    self = [super init];
    if (self) {
        // Create the data model.
        /*
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        _pageData = [[dateFormatter monthSymbols] copy];
         */
        NSMutableArray *imageViews = [[NSMutableArray alloc] init];

        FirstViewController *firstViewController = [[FirstViewController alloc] init];
        firstViewController.view.frame = CGRectMake([UIScreen mainScreen].bounds.origin.x,[UIScreen mainScreen].bounds.origin.y,[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
        [imageViews addObject:firstViewController];


        for (int i=1; i<=19; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"picture%i.jpg", i]]];

            imageView.frame = CGRectMake([UIScreen mainScreen].bounds.origin.x,[UIScreen mainScreen].bounds.origin.y,[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
            [imageViews addObject:imageView];
        }
        _pageData = imageViews;
    }
    return self;
}

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{   
    // Return the data view controller for the given index.
    if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
    dataViewController.dataObject = [self.pageData objectAtIndex:index];
    if(index == 0)
    {
        //[dataViewController addChildViewController:[self.pageData objectAtIndex:index]];
        FirstViewController *firstViewController = [self.pageData objectAtIndex:index];
        [dataViewController.view addSubview:firstViewController.view];
    }
    else
    {
        UIImageView *imageView = [self.pageData objectAtIndex:index];
        [dataViewController.view addSubview:imageView];
    }
    return dataViewController;
}
4

2 回答 2

1

您没有提供足够的上下文来确定,但看起来您最初需要从情节提要中加载的是 的实例FirstViewController,而不是 的实例UIView,因此您需要做这样的事情而不是您现在的情况正在做:

FirstViewController *firstViewController = [self.storyboard instantiateInitialViewController];

// Note: If you need to adjust the frame of the controller's view, use the screen's
// application frame rather than its bounds.
controller.view.frame = [UIScreen mainScreen].applicationFrame;

此外,这似乎是个坏主意,因为您稍后会用以下实例填充数组的其余部分UIImageView

[imageViews addObject:firstViewController];

为什么不将控制器的视图添加到数组中?然后,您可以消除viewControllerAtIndex:storyboard:方法中的条件逻辑。此外,尚不清楚为什么您将故事板作为参数传递而不是使用内置属性,或者就此而言,您发布的代码属于哪个类,以及它与您应用程序的其余部分的关系是什么. 也许您可以提供更多有关此的信息。

于 2011-12-24T21:26:35.530 回答
1

我通过为 FirstViewController 添加一个 xib 文件来让它工作。我在 xib 中设计的任何东西都显示得很好。以前我在故事板中设计它。

于 2011-12-25T04:57:07.127 回答