gpt4 book ai didi

ios - 在方向更改时重新加载 UIPageViewController View

转载 作者:可可西里 更新时间:2023-11-01 04:25:29 25 4
gpt4 key购买 nike

对于我正在构建的应用程序,我在 UIView 中有一个 UIPageViewController,它具有 Transition Style = Scroll。每个页面都有相同的 UITableViewController 类(当然有不同的数据)。此时一切正常。

现在我想要根据设备的方向使用 UITableViewController 的横向版本或纵向版本。为此,我实现了一个 NSNotification,其中包含一个在方向更改 (orientationChanged) 时调用的方法,这也有效。但是从这里出现了一个问题,当改变设备的方向时,我不确定如何加载正确的 UITableViewController 实例。如您所见,我正在使用 UIPageViewControllerDataSource 的委托(delegate)方法 viewControllerAtIndex,我在其中确保启动 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;
}
}

最佳答案

每次方向改变你都需要重新设置 View Controller

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

关于ios - 在方向更改时重新加载 UIPageViewController View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com