在方向更改时重新加载UIPageViewController视图

时间:2013-12-31 10:49:09

标签: ios iphone objective-c uitableview

对于我正在构建的应用,我UIPageViewController内有UIView Transition Style = Scroll。每个页面都具有相同的UITableViewController类(当然具有不同的数据)。在这一点上一切正常。

现在我希望拥有UITableViewController的横向版本或纵向版本,具体取决于设备的方向。为此,我实现了一个NSNotification,其方法是在方向更改(orientationChanged)上调用,这也有效。但是从这里出现问题,当改变设备的方向时我不知道如何加载正确的UITableViewController实例。如您所见,我正在使用viewControllerAtIndex的委托方法UIPageViewControllerDataSource,其中我确保启动viewController的横向或纵向版本。

问题是,在更改设备的方向时,只有在向左/向右扫描几页后才会加载正确的UITableViewController。我必须忽视一些简单的事情,但似乎无法找到它!

我有以下代码片段,其中我认为问题是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create the data model
    _pageTitles = @[@"1",@"2",@"3",@"4",@"5"];
    _pageTables = @[@"test1",@"test2",@"test3",@"test4",@"test5"];

    // Create page view controller
    self.weightPageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageViewController"];
    self.weightPageViewController.dataSource = self;

    WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:0];
    NSArray *viewControllers = @[startingViewController];
    [self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:_weightPageViewController];
    [self.view addSubview:_weightPageViewController.view];
    [self.weightPageViewController didMoveToParentViewController:self];

    self.edgesForExtendedLayout = UIRectEdgeNone;

    //Register for device orientation changes.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}

- (void) orientationChanged:(id)object {
    [self viewControllerAtIndex:0];
}

- (WeightPageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
        NSLog(@"Orientation is now Landscape);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewControllerLandscape"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else if (deviceOrientation == UIDeviceOrientationPortrait) {
        NSLog(@"Orientation is now Portrait);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    } else {
        NSLog(@"Orientation is now unknown);
        // Create a new view controller and pass suitable data.
        WeightPageContentViewController *weightPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WeightPageContentViewController"];
        weightPageContentViewController.tableText = self.pageTables[index];
        weightPageContentViewController.titleText = self.pageTitles[index];
        weightPageContentViewController.pageIndex = index;
        return weightPageContentViewController;
    }
}

2 个答案:

答案 0 :(得分:6)

每次方向改变时,您需要再次设置视图控制器

WeightPageContentViewController *startingViewController = [self viewControllerAtIndex:currentViewControllerIndex];
NSArray *viewControllers = @[startingViewController];
[self.weightPageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

答案 1 :(得分:2)

对我来说,这个简单的解决方案有效:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self.pageController.view setNeedsLayout];
}