ios - Xcode 8 swift 3 : Modal presentation transitioning delegate not called

标签 ios swift animation

问题

“DrinkTransitioningDelegate”中的委托(delegate)函数未被调用。 “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 提出了问题 here
  • 针对 iOS 9 提出了问题 here

最佳答案

解决方案

可选的协议(protocol)函数现在很重要。

委托(delegate)完全由可选函数组成,因此没有警告。

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

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
}

关于ios - Xcode 8 swift 3 : Modal presentation transitioning delegate not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39497771/

相关文章:

objective-c - 提取 Objective-c 二进制文件

iphone - 了解 NSCalendar 单位(请像我五岁一样解释)

swift - WebView嵌入youtube视频位置

ios - 准备转至导航 Controller

javascript - 如何创建包含 3D 空间中动画的 2D 对象的图形 Canvas ?

jquery - 悬停时动画不透明度 (jQuery)

ios - UIViewController 在触摸之前加载缓慢

ios - 将键盘移至 Tab ViewController TabBar 上方

swift - Compact SWIFT Reduce 闭包

javascript - 使用 CSS 或 JavaScript 循环椭圆形对象