我正在构建一个应用程序,最近发现了由传统 segues 引起的巨大内存泄漏。因此,我了解了 unwind segue。如果我简单地使用,一切都很好:
@IBAction func prepareForUnwindToMainFromFriends(segue: UIStoryboardSegue) {
}
内存泄漏得到解决,“一切都很棒”。但是从 UI 的角度来看,这个解决方案看起来很难看。所以我从这个网站实现了这个功能。并稍作改动。
override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {
return UIStoryboardSegue(identifier: identifier, source: fromViewController, destination: toViewController) {
let fromView = fromViewController.view
let toView = toViewController.view
if let containerView = fromView.superview {
let initialFrame = fromView.frame
var offscreenRect = initialFrame
var offscreenRectFinal = initialFrame
offscreenRect.origin.x += CGRectGetWidth(initialFrame)
offscreenRectFinal.origin.x -= CGRectGetWidth(initialFrame)
toView.frame = offscreenRect
containerView.addSubview(toView)
let duration: NSTimeInterval = 1.0
let delay: NSTimeInterval = 0.0
let options = UIViewAnimationOptions.CurveEaseInOut
let damping: CGFloat = 0.9
let velocity: CGFloat = 4.0
UIView.animateWithDuration(duration, delay: delay, usingSpringWithDamping: damping,
initialSpringVelocity: velocity, options: options, animations: {
toView.frame = initialFrame
fromView.frame = offscreenRectFinal
}, completion: { finished in
fromView.removeFromSuperview()
if let navController = toViewController.navigationController {
navController.popToViewController(toViewController, animated: false)
}
})
}
}
}
但现在我收到一条错误消息:
2015-05-12 08:47:31.841 PING0.4[4343:1308313] Warning: Attempt to present <NotificationViewController: 0x177030b0> on <PING0_4.ViewController: 0x16271000> which is already presenting <NotificationViewController: 0x1a488170>
我在我的应用程序中被阻止了。我可以从 VC1 到 VC2,然后再回到 VC2,但我不能再回到 VC1。看来我只能使用一次这个segue。
有人知道发生了什么吗?