UINavigationControllerDelegate 不起作用
Posted
技术标签:
【中文标题】UINavigationControllerDelegate 不起作用【英文标题】:UINavigationControllerDelegate doesn't work 【发布时间】:2015-10-27 04:03:32 【问题描述】:更新到正确的版本,既然得到了答案。
我想在导航视图控制器弹出/push时做淡入/淡出动画,然后我实现BaseViewController
:
class BaseViewController: UIViewController, UINavigationControllerDelegate
override func viewWillAppear(animated: Bool)
super.viewWillAppear(animated)
self.navigationController?.delegate = self
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
if (operation == UINavigationControllerOperation.Push)
return FadeInAnimator()
if (operation == UINavigationControllerOperation.Pop)
return FadeOutAnimator()
return nil;
和FadeInAnimator
、FadeOutAnimator
:
class FadeInAnimator: NSObject, UIViewControllerAnimatedTransitioning
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
return 0.5
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
transitionContext.containerView()?.addSubview(toViewController!.view)
toViewController?.view.alpha = 0.0
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: () -> Void in
toViewController?.view.alpha = 1.0
) (finished) -> Void in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
class FadeOutAnimator: NSObject, UIViewControllerAnimatedTransitioning
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
return 0.5
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
transitionContext.containerView()?.insertSubview((toViewController?.view)!, belowSubview: (fromViewController?.view)!)
UIView.animateWithDuration(self.transitionDuration(transitionContext), animations: () -> Void in
fromViewController?.view.alpha = 0.0
) (finished) -> Void in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
然后,我添加 2 个视图控制器并嵌入导航视图控制器,ViewControllerA
和 ViewControllerB
:
class ViewControllerA: BaseViewController
override func viewDidLoad()
super.viewDidLoad()
@IBAction func pressButton(sender: UIButton)
if let vc = create(ViewControllerB)
self.navigationController?.pushViewController(vc, animated: true)
class ViewControllerB: BaseViewController
override func viewDidLoad()
super.viewDidLoad()
按ViewControllerA
上的按钮将创建并跳转到ViewControllerB
,然后按ViewControllerB
上的导航栏项目“返回”将返回到ViewControllerA
。
但它不起作用,当按下按钮时,ViewControllerA
显示ViewControllerB
,然后再次显示ViewControllerA
。
有什么建议吗?我的 xcode 是 7.1,在 iPad Air 2 模拟器上运行。
【问题讨论】:
【参考方案1】:在动画完成块的 FadeInAnimator 中
改一下
transitionContext.completeTransition(transitionContext.transitionWasCancelled())
到
transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
【讨论】:
更改为 follow 后,现在可以正常工作:transitionContext.completeTransition(!transitionContext.transitionWasCancelled())以上是关于UINavigationControllerDelegate 不起作用的主要内容,如果未能解决你的问题,请参考以下文章