【发布时间】:2016-04-01 16:10:14
【问题描述】:
在我的 UIStoryboardSegue 子类中,我在 perform 方法的末尾使用了 presentViewController,这会导致 viewDidAppear/viewWillAppear 被调用两次?
如何防止这种情况发生?
谢谢
当前代码:
override func perform() {
// Assign the source and destination views to local variables
let sourceView = sourceViewController.view as UIView!
let destView = destinationViewController.view as UIView!
// Get the screen width and height
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
// Specify the initial position of the destination view
destView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)
// Add the destination view to the window
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(destView, aboveSubview: sourceView)
// Animate the transition
UIView.animateWithDuration(0.7, animations: { () -> Void in
// Scale down the source view
sourceView.transform = CGAffineTransformScale(sourceView.transform, 0.90, 0.90)
}) { (Finished) -> Void in
}
UIView.animateWithDuration(1.0, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.7, options: .CurveEaseOut, animations: { () -> Void in
destView.frame = CGRectOffset(destView.frame, 0.0, -screenHeight)
}) { (finished) -> Void in
self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
}
}
我现在看到,如果没有 presentViewController,新控制器在动画自定义 segue 之后很快就会被销毁。我明白我们为什么需要它。
【问题讨论】:
-
当你在destination-VC的viewDidAppear中添加print("(unsafeAddressOf(self))")时,会打印出相同的地址吗?
-
是的(而且很酷)。试了 3 次。
-
好的,现在将运行 appcoda 代码,然后编辑我的答案
标签: ios view call viewwillappear