Xcode 8 Swift 3:未调用模态表示转换委托 [英] Xcode 8 Swift 3: Modal presentation transitioning delegate not called

查看:69
本文介绍了Xcode 8 Swift 3:未调用模态表示转换委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

DrinkTransitioningDelegate中的委托函数未调用。 'td'实例在演示文稿的生命周期之内和之外都保留在内存中。

The delegate functions within 'DrinkTransitioningDelegate' are not called. The 'td' instance remains in memory during and beyond the lifecycle of the presentation.

class PresentingViewController: UIViewController {

    let td = DrinkTransitioningDelegate()

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = inventory.beverages[indexPath.row]
        item.isSelected = true
        let controller = DrinkViewController(item: item)
        controller.delegate = self
        controller.transitioningDelegate = td
        controller.modalPresentationStyle = .custom
        //let navCon = UINavigationController(rootViewController: controller)
        //navCon.transitioningDelegate = td
        //navCon.modalPresentationStyle = .custom
        present(controller, animated: true)
    }

}

class DrinkTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
        return DrinkPresentationViewController(presentedViewController:presented, presenting: presenting)
    }

    let animationController = DrinkAnimatedTransition()

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animationController.isPresentation = true
        return animationController
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animationController.isPresentation = false
        return animationController
    }

    deinit {
        print("adf")
    }

}

历史

  • The question was raised for iOS 7 here
  • The question was raised for iOS 9 here

推荐答案

可选协议功能现在已经成为现实。

Optional protocol functions are now a thing.

委托完全由可选功能组成,因此没有警告。

The delegate is comprised completely of optional functions, so there were no warnings.

这些函数作为我自己的自定义函数出现在编译器中。

These functions appeared to the compiler as my own custom functions.

func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
    return DrinkPresentationViewController(presentedViewController:presented, presenting: presenting)
}

let animationController = DrinkAnimatedTransition()

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    animationController.isPresentation = true
    return animationController
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    animationController.isPresentation = false
    return animationController
}

这些是正确的功能。

func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
    return DrinkPresentationViewController(presentedViewController:presented, presenting: presenting)
}

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let animationController = DrinkAnimatedTransition()
    animationController.isPresentation = true
    return animationController
}

func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let animationController = DrinkAnimatedTransition()
    animationController.isPresentation = false
    return animationController
}

这篇关于Xcode 8 Swift 3:未调用模态表示转换委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆