Xcode 8 Swift 3: Modal presentation transitioning delegate not called

Issue

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

  • iOS: how to perform a HTTP POST request?
  • Adding a pin annotation to a map view on a long press in swift
  • How to decode / convert a base64 string to NSData?
  • How to get UITableViewCell indexPath from the Cell?
  • Making an asynchronous call prior to a segue to the next view controller (Swift)
  • How to know if the iOS App you build is for 64 bit.
  • 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")
        }
    
    }
    

    History

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

    Solutions Collect From Internet About “Xcode 8 Swift 3: Modal presentation transitioning delegate not called”

    Solution

    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
    }
    

    These are the correct functions.

    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
    }