Changing UIPageViewController's page programmatically doesn't update the UIPageControl

后端 未结 7 1739
悲哀的现实
悲哀的现实 2020-12-09 17:39

In my custom UIPageViewController class:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
           


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 18:19

    As mentioned in accepted answer, you need to implement

    - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
    

    But for me it was enough to use it like this:

    Objective-C:

    - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
        // The selected item reflected in the page indicator.
        return [self.controllers indexOfObject:[pageViewController.viewControllers firstObject]];
    }
    

    Swift 3+:

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
            guard let index = viewControllers?.index(of: (pageViewController.viewControllers?.first)!) else { return 0 }
            return index
    }
    

    Without the need to remember the current index. Where self.controllers is an NSArray of UIViewControllers displayed in given UIPageViewController. I'm not sure how exactly your BSTCMWelcomingPageViewModel works, but it should be easy to adjust.

提交回复
热议问题