如何为 UIPageViewController Delegate 制作自定义类?

Posted

技术标签:

【中文标题】如何为 UIPageViewController Delegate 制作自定义类?【英文标题】:How to make a custom class for UIPageViewControllerDelegate? 【发布时间】:2019-02-07 15:07:04 【问题描述】:

我正在编写一个带有 UIPageViewControllerios 应用程序。我的根视图控制器是ViewController,我在它下面添加了UIPageViewController 作为子VC。

最初,我将我的ViewController 设为页面视图 VC 委托: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) 根本没有被调用,尽管我确实将委托设置为这个新类的实例。

我唯一能想到的是,新的委托对象不再是 VC(它曾经是我的主要 VC)。所以我怀疑这种行为变化与 VC 层次结构有关?

我错过了什么吗?

【问题讨论】:

一个可能的原因可能是没有人对PeoplePageViewDelegate 的实例保持强引用。请说明您是如何将委托设置为这个新类的实例 @OOPer 在这里:在ViewControllerinit 中,我做了:let dedicatedDelegateObject = PeoplePageViewDelegate(param1, param2),然后是self.pagedViewController.delegate = dedicatedDelegateObject 'OOPer 我看了一下苹果文档中的UIPageViewController.delegate,它是一个弱引用。我猜这就是原因? 确认工作。把它放在一个答案中,我会标记它。 很高兴听到您成功了。请自行发布答案。 【参考方案1】:

(这个答案的灵感来自 OOPer 的 cmets)。

发现原因是因为最初我只保留了一个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

    


【讨论】:

以上是关于如何为 UIPageViewController Delegate 制作自定义类?的主要内容,如果未能解决你的问题,请参考以下文章

如何用 UIScrollView 实现 UIPageViewController?

UIPageViewController:如何处理视图控制器的不同方向?

如何处理 UIPageViewController 中的向后滚动?

UIPageViewController 与滚动视图奇数插入反弹

图像的简单横向滚动 - UIScrollView 或 UIPageViewController?

如何使用现有 UIPageViewController 的按钮以编程方式滑动视图