ios - 如何为 UIPageViewController Delegate 创建自定义类?

标签 ios swift delegates uipageviewcontroller

我正在使用UIPageViewController编写一个iOS应用程序。我的 Root View Controller 是 ViewController,我在其下添加了 UIPageViewController 作为子 VC。

最初,我将 ViewController 作为页面 View VC 委托(delegate):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 ")
                }
            }
        }


    }
}

这个方法效果很好。直到我决定将委托(delegate)功能移至专用类:

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) 根本没有被调用,尽管我确实将委托(delegate)设置为这个新类的实例。

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

我是不是错过了什么?

最佳答案

(这个答案的灵感来自于 OOPer 的评论)。

发现原因是因为最初我只保留了对委托(delegate)对象的引用。像这样:

之前:

class ViewController:  UIViewController { 
    init() {

        self.pageViewController.delegate = PeoplePageViewDelegate(….)


    }

}

这里的问题是,一旦执行点通过了 init 方法范围,PeoplePageViewDelegate 就会被垃圾回收,因为它的引用计数为零。

我是如何解决这个问题的:

我在 ViewController 中添加了一个强引用作为字段,以便它将保留委托(delegate)对象:

之后:

class ViewController:  UIViewController { 

    var myStrongReference: PeoplePageViewDelegate?
    init() {

        self.myStrongReference = PeoplePageViewDelegate(….)

        self.pageViewController.delegate = self.myStrongReference

    }

}

关于ios - 如何为 UIPageViewController Delegate 创建自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54576324/

相关文章:

xcode - 委托(delegate)协议(protocol)不起作用,返回 nil

iphone - 将 UIImageView 添加到 UIView

iphone - CGContextSetRGBFillColor 不工作

Swift:另一个计时器在应用程序进入后台时启动

ios - Xcode - Gmail 用于从左向右滑动到下一个上一个邮件项目的过渡是什么

swift - 在 Swift 的通用结构上实现 Equatable 的奇怪行为

ios - iOS swift 中所有对象的相同引用

ios - iOS 按需资源错误 "Asset URL has expired"(AssetErrorDomain 代码 6)是什么意思?

ios - UITextField 返回键在 ViewController 中进行更改?

c# - 如何在调用构造函数之前初始化 F# 中的字段?