代码之家  ›  专栏  ›  技术社区  ›  modeller

如何为uipageViewControllerDelegate创建自定义类?

  •  0
  • modeller  · 技术社区  · 5 年前

    我正在编写一个iOS应用程序 UIPageViewController . 我的根视图控制器是 ViewController 我补充说 uipageview控制器 作为一个孩子的风投。

    最初,我 可视控制器 作为页面视图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)。所以我怀疑这种行为的改变与风投的等级制度有关?

    我错过什么了吗?

    1 回复  |  直到 5 年前
        1
  •  0
  •   modeller    5 年前

    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
    
        }
    
    }