我想通过使用此代码转到前一页,但我不能。如果你给我一个答案,感激不尽!
覆盖func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){
if segue.identifier == "edit"{
let destViewController = segue.destinationViewController as! ScoreBoardVC
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.matches = matches[indexPath.row]
let alertView = UIAlertController(title: "Want to edit?", message: "Keep in mind the date!", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
presentViewController(alertView, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
sourceViewController
可能在segue之后退出视图层次结构,因此如果在那里显示控制器,它将立即被解除。
那里的prepareForSegue可能不会在视图层次结构中出现destinationViewController
,所以如果你在那里展示它,你可能会得到
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上添加一个ivar。
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?以了解可能适用的其他答案。