UIPageViewController - 视图无法加载

时间:2011-12-24 19:41:08

标签: objective-c uiview uiimageview uipageviewcontroller

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

我是从默认的pageController模板创建的。

ModelController.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;
}

2 个答案:

答案 0 :(得分: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:方法中的条件逻辑。此外,还不清楚为什么你将故事板作为参数传递而不是使用内置属性,或者就此而言,你发布的代码所属的类是什么,以及它与你应用程序的其余部分的关系。也许你可以提供更多相关信息。

答案 1 :(得分:1)

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