2016-01-23 53 views
1

我想通过使用此代码去前一页,但我不能。欣赏,如果你给我一个答案!在segue之前的UIalertController

倍率FUNC prepareForSegue(SEGUE:UIStoryboardSegue,发件人: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

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

相关问题