gpt4 book ai didi

ios - 导航 Controller 委托(delegate)方法没有得到调用

转载 作者:可可西里 更新时间:2023-11-01 02:01:52 25 4
gpt4 key购买 nike

在下面的代码中,我试图在导航转换期间创建一个自定义动画器,但导航 Controller 委托(delegate)方法没有得到调用。请查看下面的代码并向我建议一个解决方案。

请注意,我已将 DemoTransitionAnimationViewController 嵌入到导航 Controller 中。这个 VC 在它的 View 上有一个按钮。单击此 View 时,我将推送另一个 View Controller 。但是委托(delegate)方法仍然没有得到调用。

CustomAnimator.swift

//
// CustomAnimator.swift
// LoginModule
//
// Created by Shubham Ojha on 8/14/17.
// Copyright © 2017 BBI. All rights reserved.
//

class FadeInAnimator: NSObject,
UIViewControllerAnimatedTransitioning {
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
return 0.35

}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let fromVC = transitionContext.viewController(
forKey: UITransitionContextViewControllerKey.from)
let toVC = transitionContext.viewController(
forKey: UITransitionContextViewControllerKey.to)

containerView.addSubview(toVC!.view)
toVC!.view.alpha = 0.0

let duration = transitionDuration(using: transitionContext)
UIView.animate(withDuration: duration, animations: {
toVC!.view.alpha = 1.0
toVC?.view.backgroundColor = UIColor.blue
}, completion: { finished in
let cancelled = transitionContext.transitionWasCancelled
transitionContext.completeTransition(!cancelled)
})
}

}

class NavigationControllerDelegate: NSObject,
UINavigationControllerDelegate {

func navigationController(
_ navigationController: UINavigationController,
animationControllerFor operation:
UINavigationControllerOperation,
from fromVC: UIViewController,
to toVC: UIViewController
) -> UIViewControllerAnimatedTransitioning? {

return FadeInAnimator()

}
}

DemoTransitionAnimationViewController.swift

//
// DemoTransitionAnimationViewController.swift
// LoginModule
//
// Created by Shubham Ojha on 8/15/17.
// Copyright © 2017 BBI. All rights reserved.
//

import UIKit

class DemoTransitionAnimationViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
print(self.navigationController ?? "Not exist")

if self.navigationController != nil{
self.navigationController?.delegate = NavigationControllerDelegate()
// In the above statement if I am setting the delegate as self instead of
//NavigationControllerDelegate() and conforming the methods of navigation
//controller delegate protocol. It works perfectly.
}
else{
print("navigation controller does not exist")
}

}

}

最佳答案

试试这个:

if self.navigationController != nil{
self.navigationController?.delegate = self // Update assignment here
}
else {
print("navigation controller does not exist")
}

self.navigationController?.delegate = NavigationControllerDelegate() 是一个独立的(没有任何 UIViewController 引用)内存分配。因此,它不会响应任何 View Controller 的委托(delegate)方法的实现。

self.navigationController?.delegate = self 告诉导航 Controller 委托(delegate)使用 View Controller DemoTransitionAnimationViewController 的引用并考虑其导航实现。

关于ios - 导航 Controller 委托(delegate)方法没有得到调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com