我正在创建一个iOS应用程序,它有一个UIPageViewController,如果还没有添加任何其他内容,则会加载默认页面,并且用户可以在应用程序中添加页面。但是,初始视图永远不会随着其他视图的添加而改变。
我尝试过这些问题的答案:
Is it possible to Turn page programmatically in UIPageViewController?
Changing UIPageViewController's page programmatically doesn't update the UIPageControl
Removing a view controller from UIPageViewController
Refresh a UIPageViewController
它们都不适合我。视图仍然保留在初始页面,直到我重新加载应用程序,之后,页面显示正常,我可以添加任意多个。
这是我制作PageView和页面的代码
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self pageControllerView] bounds]];
UIViewController *initialViewController = [self viewControllerAtIndex:0];
__block Page1ViewController *blocksafeSelf = self;
// This doesn't work either... :(
void (^completionBlock)(BOOL) = ^(BOOL finished)
{
// It seems that whenever the setViewControllers:
// method is called with "animated" set to YES, UIPageViewController precaches
// the view controllers retrieved from it's data source meaning that
// the dismissed controller might not be removed. In such a case we
// have to force it to clear the cache by initiating a non-animated
// transition.
usleep(1);
dispatch_async(dispatch_get_main_queue(), ^(){
[blocksafeSelf.pageController setViewControllers:@[initialViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
});
};
[self.pageController setViewControllers:@[initialViewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:completionBlock];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (void)reloadViews
{
[[self childViewControllers] makeObjectsPerformSelector:@selector(removeFromParentViewController)];
[[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.pageController willMoveToParentViewController:nil];
[self.pageController removeFromParentViewController];
[self.pageController.view removeFromSuperview];
self.pageController = nil;
self.pageController = [[UIPageViewController alloc] init];
[self.view setNeedsDisplay];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((SomethingViewController *)viewController).index;
index++;
if (index >= [Settings somethings].count)
{
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Storyboard_iPhone"
bundle:nil];
if ([Settings somethings].count == 0 && index == 0)
{
NoSomethingViewController *vc = [sb instantiateViewControllerWithIdentifier:@"NoSomethingViewController"];
vc.index = index;
return vc;
}
SomethingViewController *childViewController = [sb instantiateViewControllerWithIdentifier:@"SomethingViewController"];
childViewController.something = [[Settings somethings] somethingAtIndex:index];
childViewController.index = index;
return childViewController;
}
当应该创建一个新页面时,通过在something
数组中添加somethings
,我从View控制器调用re something
来添加viewControllerAtIndex
。然后代码点击{{1}}的第二部分,但是我在手机上看到的页面上没有显示,因为我仍然看到NoSomethingViewController和它的视图。
如果您需要更多代码,我会提供。
答案 0 :(得分:3)
在pageViewController viewWillAppear方法
中添加以下代码dataSource = self
并在viewWillDisappear中添加
dataSource = nil
这将强制pageViewController重新加载其dataSource
答案 1 :(得分:0)
要显示当前点,请不要忘记您必须更新页面指示符
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
如何更新UIPageControl指标https://stackoverflow.com/a/22576250/3897011
的示例答案 2 :(得分:0)
经过很长一段时间,在浪费了与Apple的支持票之后,我决定采用另一种方法。
在我的故事板中,我创建了2个不同的导航控制器,每个情况一个(如果有somethings
,如果没有),我有viewDidAppear
的初始视图控制器,实例化其中一个并呈现它。这样,我确保整个导航控制器正确重新加载。
UINavigationController *nc;
if ([Settings somethings].count)
{
nc = [self.storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];
}
else
{
nc = [self.storyboard instantiateViewControllerWithIdentifier:@"NoSomethingNavigationController"];
}
self.nc = nc;
[self presentViewController:nc animated:NO completion:nil];
答案 3 :(得分:-1)
要重新加载UIPageViewController
,您可以使用以下代码:
[self.pageController setViewControllers:@[yourCurrentController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
还请实施
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
方法