2016-01-23 75 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?見,可能在這裏工作的其他答案。

相關問題