Как сделать пользовательский класс для UIPageViewControllerDelegate? - PullRequest
0 голосов
/ 07 февраля 2019

Я пишу приложение для iOS с UIPageViewController.Мой контроллер корневого представления ViewController, и я добавил UIPageViewController в качестве дочернего VC под ним.

Изначально я сделал свой ViewController делегатом ВК для просмотра страницы: UIPageViewControllerDelegate:

extension ViewController: UIPageViewControllerDelegate {



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

        // set the pageControl.currentPage to the index of the current viewController in pages
        if let viewControllers = pageViewController.viewControllers as? [UIViewController] {
            if let viewControllerIndex = self.detailPagedVC.pages.index(of: viewControllers[0]) {
                self.detailPagedVC.pageControl.currentPage = viewControllerIndex

                // if current page is a single person view controller, zoom to that person's face
                if let singlePersonViewController = self.detailPagedVC.pages[viewControllerIndex] as? SinglePersonPageViewController {

                    // print("didFinishAnimating: \(viewControllerIndex)")
                    self.zoomableImageVC.zoomableImageView.zoom(to: self.identificationResults[viewControllerIndex].face.rect, with: Constants.contentSpanRatio, animated: true)
                } else if let summaryPageViewController = self.detailPagedVC.pages[viewControllerIndex] as? SummaryPageViewController {
                    self.zoomableImageVC.zoomableImageView.zoom(to: self.zoomableImageVC.zoomableImageView.imageView.bounds, with: Constants.contentSpanRatio, animated: true)
                } else {
                    print("gw: err: unkown type of page controller in paged view ")
                }
            }
        }


    }
}

Этот способ работает нормально.Пока я не решил переместить делегирующую функцию в выделенный класс:

class PeoplePageViewDelegate: NSObject, UIPageViewControllerDelegate{

    private struct Constants {

        // the ratio of the content (e..g face) taken inside the entire view
        static let contentSpanRatio: CGFloat = 0.8

    }

    // store a reference to the object which will take the actual action
    // action 1: zooming
    weak var zoomingActionTaker: ZoomableImageView?

    // action 2: paging
    weak var pagingActionTaker: PeoplePageViewController?


    // the delegator who relies on this object
    unowned let delegator: PeoplePageViewController

    init(delegator: PeoplePageViewController) {
        self.delegator = delegator

        // wire back to delegator 
        // self.pagingActionTaker = delegator // gw: mind the nuance difference, you chain a weak ref on a unowned ref, what can go wrong?

        super.init()
    }




    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        print("gw:0")
        // set the pageControl.currentPage to the index of the current viewController in pages
        if let viewControllers = pageViewController.viewControllers as? [UIViewController] {
            print("gw:1")
            if let viewControllerIndex = self.delegator.pages.index(of: viewControllers[0]) {
                 print("gw:2")
                self.pagingActionTaker?.pageControl.currentPage = viewControllerIndex

                // if current page is a single person view controller, zoom to that person's face
                if let singlePersonViewController = self.delegator.pages[viewControllerIndex] as? SinglePersonPageViewController {

                     print("gw:3")
                    self.zoomingActionTaker?.zoom(to: singlePersonViewController.identification.face.rect, with: Constants.contentSpanRatio, animated: true)


                } else if let summaryPageViewController = self.delegator.pages[viewControllerIndex] as? SummaryPageViewController,

                    let entireImageBounds = self.zoomingActionTaker?.imageView.bounds {
                    print("gw:4")
                    self.zoomingActionTaker?.zoom(to: entireImageBounds, with: Constants.contentSpanRatio, animated: true)
                } else {
                    print("gw: err: unkown type of page controller in paged view ")
                }
            }
        }


    }
}

Это вызывает проблему: функция pageViewController(_: didFinishAnimating:previousViewControllers:transitionCompleted) вообще не вызывается, несмотря на то, что я установил делегат для экземпляра этого нового класса.

Единственное, о чем я могу думать, это то, что новый объект делегата больше не является виртуальным венцом (раньше он был моим основным виртуальным контроллером).Поэтому я подозреваю, что это изменение поведения связано с иерархией ВК?

Я что-то пропустил?

1 Ответ

0 голосов
/ 08 февраля 2019

(Этот ответ основан на комментариях ООП).

Было обнаружено, что причина в том, что изначально я сохраняю только ссылку weak на объект делегата.Например:

До:

class ViewController:  UIViewController { 
    init() {

        self.pageViewController.delegate = PeoplePageViewDelegate(….)


    }

}

Проблема здесь в том, что когда точка выполнения прошла область действия метода init, PeoplePageViewDelegate собирается мусором, поскольку его счетчик ссылок равен нулю..

Как я это исправил:

Я добавил сильную ссылку в качестве поля в ViewController, чтобы он сохранил объект делегата:

После:

class ViewController:  UIViewController { 

    var myStrongReference: PeoplePageViewDelegate?
    init() {

        self.myStrongReference = PeoplePageViewDelegate(….)

        self.pageViewController.delegate = self.myStrongReference

    }

}
...