Ios 未调用UINavigationControllerDelegate方法
当从自定义Ios 未调用UINavigationControllerDelegate方法,ios,swift,animation,uinavigationcontroller,uikit,Ios,Swift,Animation,Uinavigationcontroller,Uikit,当从自定义UINavigationController类中推/弹出ViewController时,我尝试进行自定义转换。我正在实现UINavigationControllerDelegate方法 navigationController(\uu:animationControllerFor:from:to:),但不会调用它 我正在故事板中创建一个UINavigationController,并将其类设置为CustomNavigationController。我还在故事板中为它分配了一个根View
UINavigationController
类中推/弹出ViewController时,我尝试进行自定义转换。我正在实现UINavigationControllerDelegate
方法
navigationController(\uu:animationControllerFor:from:to:)
,但不会调用它
我正在故事板中创建一个UINavigationController
,并将其类设置为CustomNavigationController
。我还在故事板中为它分配了一个根ViewController(让我们调用根VCcustomviewcontrolleroot
)
以下是我正在使用的代码(简化且未经测试):
协议导航委托{
func pushCustomViewController()
func popViewController()
}
类CustomNavigationController:UINavigationController,NavigationDelegate{
init(){
super.init(nibName:nil,bundle:nil)
代表=自我
}
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
}
重写func viewDidLoad(){
self.navigationBar.ishiden=true
guard viewControllers.first是CustomViewControllerRoot else{fatalError(“错误”)}
rootVC=viewControllers.first as?CustomViewControllerRoot
rootVC?.navigationDelegate=self
//设置要使用的其余ViewController
customVC=CustomUIViewController()
customVC?.navigationDelegate=self
}
var rootVC:CustomViewControllerRoot?
var customVC:CustomViewController?
func pushCustomViewController(){
如果customVC!=nil{
self.pushViewController(customVC!,动画:true)
}
}
func popViewController(){
self.popViewController(动画:true)
}
}
扩展CustomNavigationController:UINavigationControllerDelegate{
func navigationController(navigationController:UINavigationController,AnimationController用于操作:UINavigationController.operation,从VC:UIViewController到VC:UIViewController)->UIViewControllerAnimatedTransitioning{
//即使委托是在CustomNavigationController的初始值设定项中设置的,也不会调用它
打印(“测试”)
归零
}
}
然后,我让导航层次结构中的每个自定义UIViewController
子类委托推送或弹出到上面的CustomNavigationController
。例如,这是分配给导航控制器的根vc。由于它位于根目录下,因此无需推送或弹出,正如在显示CustomNavigationController
时所显示的那样。当它发现另一个VC应该出现在它上面时,它将委托给CustomNavigationController
:
class CustomViewControllerRoot{
var navigationDelegate:navigationDelegate?
重写func viewDidLoad(){
super.viewDidLoad()
}
@objc func someButtonPressedToPresentCustomVC()按钮{
navigationDelegate?.pushCustomViewController()
}
}
解雇在每个CustomViewController
内部处理,该控制器还将弹出窗口委托给CustomNavigationController
(我不想使用导航栏进行解雇,因此从一开始就没有“后退按钮”):
据我所知,
CustomNavigationController
扩展中的UINavigationControllerDelegate
方法应该在执行推送或弹出操作时调用,因为我正在初始值设定项中将delegate
变量设置为self
。您的导航控制器应该具有根视图控制器。
然后,您应该从根视图控制器推送自定义视图控制器。和委托方法调用
导航控制器代码:导入UIKit
class CustomNV: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
extension CustomNV: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
print("TEST")
return nil
}
}
RootViewController代码:
class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let viewController = UIViewController(nibName: nil, bundle: nil)
viewController.view.backgroundColor = .green
navigationController?.pushViewController(viewController, animated: true)
}
}
在情节提要中将根视图控制器设置为导航控制器的根!目前,我通过委托CustomNavigationController本身来完成所有推送和弹出操作,但我应该在根vc?@A.Claesson中完成所有这些操作,最好在视图控制器中完成,或者在视图控制器中完成一些对象帮助器(一些动画制作者)并将其插入视图控制器中(这是一种更困难的方法).我添加了一些代码来显示我的层次结构。所有的push/pop都应该可以工作,但是从未调用
UINavigationControllerDelegate
中的委托方法,我不明白为什么
class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let viewController = UIViewController(nibName: nil, bundle: nil)
viewController.view.backgroundColor = .green
navigationController?.pushViewController(viewController, animated: true)
}
}