的sourceViewController
很可能被退出賽格瑞後視圖層次結構,因此,如果控制器被呈現在那裏,這將是立即解散。
的destinationViewController
可能不存在由prepareForSegue有視圖層次,所以如果你把在那裏,你可能會得到
2016-08-23 10:22:22.354 Foo[589:137407] Warning: Attempt to present <UIAlertController: 0x17eb0e00> on <Foo.DestinationViewController: 0x177e7b60> whose view is not in the window hierarchy!
2016-08-23 10:22:22.904 Foo[589:137407] Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x17eb0e00>)
一種可能性是添加伊娃到您destinationViewController像這樣。
class DestinationViewController: UIViewController {
var presentWhenAppearing: UIViewController? = nil
override func viewWillAppear(animated: Bool) {
if let p = presentWhenAppearing {
self.presentViewController(p, animated: true, completion: nil)
presentWhenAppearing = nil
}
}
}
然後可以內部
prepareForSegue
如下使用
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "myIdentifier" {
guard let destinationVC = segue.destinationViewController as? DestinationViewController else {
fatalError("Unexpected VC")
}
let alert = UIAlertController(title: "You are currently offline", message: "but your download willl be begin and continue when you have an internet connection.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
destinationVC.presentWhenAppearing = alert
}
}
也How to present UIAlertController when not in a view controller?見,可能在這裏工作的其他答案。