在 UIViewControllerAnimatedTransitioning 委托中,transitionContext.fromViewController 为 nil
Posted
技术标签:
【中文标题】在 UIViewControllerAnimatedTransitioning 委托中,transitionContext.fromViewController 为 nil【英文标题】:Inside UIViewControllerAnimatedTransitioning delegate, the transitionContext.fromViewController is nil 【发布时间】:2016-05-20 18:19:26 【问题描述】:我有一个 UINavigationController 设置,并希望使用自定义动画来弹出/推送视图。我之前使用过自定义转换没有问题,但在这种情况下,我实际上在我的“from”和“to”UIViewControllers中找到了 nil 值。
我的设置与SO Post非常相似
自定义 DataEntryViewController
class DataEntryViewController : UIViewController, DataEntryViewDelegate, UINavigationControllerDelegate
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
let animator = DataEntryTransitionAnimator()
animator.duration = 2
return animator
自定义 BaseTransitionAnimator
class BaseTransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning
var duration : NSTimeInterval = 0.5 // default transition time of 1/2 second
var appearing : Bool = true // is the animation appearing (or disappearing)
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
return duration
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
assert(true, "animateTransition MUST be implemented by child class")
子类TransitionAnimator
class DataEntryTransitionAnimator : BaseTransitionAnimator
override func animateTransition(transitionContext: UIViewControllerContextTransitioning)
let containerView = transitionContext.containerView()
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey) as! DataEntryViewController
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey) as! DataEntryViewController
let duration = transitionDuration(transitionContext)
// do fancy animations
使用上述方法,fromVC 和 toVC 都为零
transitionContext 怎么可能没有指向 'to' 和 'from' UIViewControllers 的有效指针?
【问题讨论】:
【参考方案1】:您正在使用UITransitionContextFromViewKey
,但您需要使用UITransitionContextFromViewControllerKey
“到”也一样
【讨论】:
:facepalm: 哈,谢谢。我在命令行上调试并且也在查看调试窗口,发誓它对 VC 显示为 nil,但可能是由于可选的,它看起来很有趣。还在习惯 Swift。 别担心!简单的错字,很容易漏掉。 ?以上是关于在 UIViewControllerAnimatedTransitioning 委托中,transitionContext.fromViewController 为 nil的主要内容,如果未能解决你的问题,请参考以下文章
在 React 应用程序中在哪里转换数据 - 在 Express 中还是在前端使用 React?