在UIPageViewController子级上未调用viewWillAppear

时间:2018-09-28 12:02:25

标签: ios swift uiviewcontroller uipageviewcontroller

所以我有UIPageViewController,上面有2个UIViewControllers。我想知道何时显示它们中的每一个,但是切换它们时不会调用viewWillAppear或didAppear。除了在UIPageViewController中进行滑动检查之外,没有人知道为什么不这样做或如何知道何时滑动?

class HomePageViewController: UIPageViewController {

weak var homePageDelegate: HomePageViewControllerDelegate?

var sequenceControl: PageContainerViewController!
var chatControl: PageContainerViewController!
var directControl: PageContainerViewController!
var lastShownControl: PageContainerViewController!

private var partnerConnectionChanged = true

private(set) lazy var soloViewControllers: Array<UIViewController> = {
    return loadViews(for: .solo)
}()

private(set) lazy var partnerViewControllers: Array<UIViewController> = {
    return loadViews(for: .partner)
}()

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = self
    delegate = self

    WebSocketController.sharedInstance.add(self)

    prepareViews()

    if let initialViewController = getViewsList().first {
        scrollToViewController(viewController: initialViewController)
    }

    homePageDelegate?.homePageViewController(homePageViewController: self, didUpdatePageCount: getViewsList().count)
}

override func viewWillAppear(_ animated: Bool) {
    if (FlowController.sharedInstance.showDeviceConnect) {
        navigationController?.popViewController(animated: false)
    }
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

func prepareViews(){
    sequenceControl = storyboard?.instantiateViewController(withIdentifier: "pageContainerIdentifier") as? PageContainerViewController
    sequenceControl.setViewOption(to: .sequenceView, with: self)
    chatControl = storyboard?.instantiateViewController(withIdentifier: "pageContainerIdentifier") as? PageContainerViewController
    chatControl.setViewOption(to: .chatView, with: self)
    directControl = storyboard?.instantiateViewController(withIdentifier: "pageContainerIdentifier") as? PageContainerViewController
    directControl.setViewOption(to: .directControlView, with: self)
}

func loadViews(for arrayType: ArrayType) -> Array<UIViewController>{
    var viewContainers = Array<UIViewController>()
    viewContainers.append(sequenceControl)
    if(arrayType == .partner){
        viewContainers.append(chatControl)
    }
    viewContainers.append(directControl)
    return viewContainers
}

func getViewsList() -> [UIViewController]{
    if (WebSocketController.sharedInstance.partnerConnected) {
        return partnerViewControllers
    } else {
        return soloViewControllers
    }
}

 //MARK: - Page Controller

/**
 Scrolls to the next view controller.
 */
public func scrollToNextViewController() {
    if let visibleViewController = viewControllers?.first,
        let nextViewController = pageViewController(self, viewControllerAfter: visibleViewController) {
        scrollToViewController(viewController: nextViewController)
    }
}

/**
 Scrolls to the previous view controller.
 */
public func scrollToPreviousViewController() {
    if let visibleViewController = viewControllers?.last,
        let previousViewController = pageViewController(self, viewControllerBefore: visibleViewController) {
        scrollToViewController(viewController: previousViewController, direction: .reverse)
    }
}

/**
 Scrolls to the view controller at the given index. Automatically calculates
 the direction.

 - parameter newIndex: the new index to scroll to
 */
func scrollToViewController(index newIndex: Int) {
    if let firstViewController = viewControllers?.first,
        let currentIndex = getViewsList().index(of:firstViewController) {
        let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
        let nextViewController = getViewsList()[newIndex]
        scrollToViewController(viewController:nextViewController, direction: direction)
    }
}

/**
 Scrolls to the given 'viewController' page.

 - parameter viewController: the view controller to show.
 */
private func scrollToViewController(viewController: UIViewController,
                                    direction: UIPageViewControllerNavigationDirection = .forward) {
    setViewControllers([viewController],
                       direction: direction,
                       animated: true,
                       completion: { (finished) -> Void in
                        // Setting the view controller programmatically does not fire
                        // any delegate methods, so we have to manually notify the
                        // 'tutorialDelegate' of the new index.
                        self.notifyHomePageDelegateOfNewIndex()
                        self.lastShownControl = viewController as? PageContainerViewController
                        self.showTutorial()
    })
}

/**
 Notifies '_tutorialDelegate' that the current page index was updated.
 */
private func notifyHomePageDelegateOfNewIndex() {
    if let firstViewController = viewControllers?.first,
        let index = getViewsList().index(of:firstViewController) {
        homePageDelegate?.homePageViewController(homePageViewController: self, didUpdatePageIndex: index)
    }
}
}

// MARK: UIPageViewControllerDataSource

extension  HomePageViewController: UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController,
                        viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = getViewsList().index(of:viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    // User is on the first view controller and swiped left to loop to
    // the last view controller.
    guard previousIndex >= 0 else {
        return nil
    }

    guard getViewsList().count > previousIndex else {
        return nil
    }

    return getViewsList()[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController,
                        viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = getViewsList().index(of:viewController) else {
        return nil
    }

    let nextIndex = viewControllerIndex + 1
    let listViewControllersCount = getViewsList().count

    // User is on the last view controller and swiped right to loop to
    // the first view controller.
    guard listViewControllersCount != nextIndex else {
        return nil
    }

    guard listViewControllersCount > nextIndex else {
        return nil
    }

    return getViewsList()[nextIndex]
}

//MARK: - WebSocket Delegat

func connectionPartnerChanged(success: Bool) {
    partnerConnectionChanged = true
}
}

extension  HomePageViewController: UIPageViewControllerDelegate {

func pageViewController(_ pageViewController: UIPageViewController,
                        didFinishAnimating finished: Bool,
                        previousViewControllers: [UIViewController],
                        transitionCompleted completed: Bool) {
    notifyHomePageDelegateOfNewIndex()
}

}

protocol  HomePageViewControllerDelegate: class {

/**
 Called when the number of pages is updated.

 - parameter homePageViewController: the TutorialPageViewController instance
 - parameter count: the total number of pages.
 */
func homePageViewController(homePageViewController:  HomePageViewController,
                            didUpdatePageCount count: Int)

/**
 Called when the current index is updated.

 - parameter homePageViewController: the TutorialPageViewController instance
 - parameter index: the index of the currently visible page.
 */
func homePageViewController(homePageViewController:  HomePageViewController,
                            didUpdatePageIndex index: Int)
}

2 个答案:

答案 0 :(得分:2)

使用此UIPageViewController Delegate method

func pageViewController(_ pageViewController: UIPageViewController, 
            willTransitionTo pendingViewControllers: [UIViewController]) {
    let controller = pendingViewControllers?.first as? SecondViewController
}

func pageViewController(_ pageViewController: UIPageViewController, 
          didFinishAnimating finished: Bool, 
     previousViewControllers: [UIViewController], 
         transitionCompleted completed: Bool) {
    if completed {

    }
}

答案 1 :(得分:1)

出现 的视图控制器调用

viewWillAppearviewDidAppear

因此,您需要在UIPageViewController本身内部的“页面”视图控制器中实现这些功能 ... 不是

如果您需要UIPageViewController来了解它,请实现willTransitionTodidFinishAnimating UIPageViewController Delegate方法,或者创建自己的协议/委托函数。