的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?见,可能在这里工作的其他答案。