Xcode 8 Swift 3:模态演示文稿转换委托不被调用

问题

“DrinkTransitioningDelegate”中的委托函数不会被调用。 在演示的生命周期中,“td”实例保留在内存中。

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

历史

  • 这里提出了iOS 7的问题
  • 这里提出了iOS 9的问题

可选的协议function现在是一件事情。

代表完全由可选function组成,所以没有警告。

这些函数在编译器中显示为我自己的自定义函数。

 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 } 

这些是正确的function。

 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 }