我正在制作一个具有 UIPageViewController 的 iOS 应用程序,如果尚未添加任何其他页面,则该 UIPageViewController 会加载默认页面,并且用户可以在通过应用程序进行时添加页面。但是,初始 View 不会随着其他 View 的添加而改变。
我尝试了以下问题的答案:
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
它们都不适合我。在我重新加载应用程序之前, View 仍保留在初始页面,之后页面显示正常,我可以添加任意数量的页面。
这是我用于制作 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] makeObjectsPerformSelectorselector(removeFromParentViewController)];
[[self.view subviews] makeObjectsPerformSelectorselector(removeFromSuperview)];
[self.pageController willMoveToParentViewController:nil];
[self.pageController removeFromParentViewController];
[self.pageController.view removeFromSuperview];
self.pageController = nil;
self.pageController = [[UIPageViewController alloc] init];
[self.view setNeedsDisplay];
}
- (UIViewController *)pageViewControllerUIPageViewController *)pageViewController viewControllerAfterViewControllerUIViewController *)viewController
{
NSUInteger index = ((SomethingViewController *)viewController).index;
index++;
if (index >= [Settings somethings].count)
{
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)viewControllerAtIndexNSUInteger)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;
}
当应该创建一个新页面时,通过在 somethings 数组中添加一个 something ,我从添加 的 View Controller 调用 reloadViews一些东西 。然后代码会到达 viewControllerAtIndex 的第二部分,但这并没有显示在我在手机上看到的页面上,因为我仍然看到 NoSomethingViewController 和它的 View 。
如果您需要更多代码,我会提供。
Best Answer-推荐答案 strong>
在pageViewController viewWillAppear方法中添加如下代码
数据源 = self
并在 View 中添加WillDisappear
数据源 = 无
这将强制 pageViewController 重新加载其数据源
关于ios - 在 UIPageViewController 中重新加载 View ,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24551073/
|