Итак, у меня есть 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)
}