2016-06-29 17 views
0

當用戶在alertView中按回車鍵時,我試圖回到viewcontroller,它向我顯示錯誤(libC++ abi.dylib:以NSException類型的未捕獲異常終止)如何在alertview中回到viewcontroller

@IBAction func roundButton(sender: AnyObject) { 

    //add alert 
    alertController = UIAlertController(title: "Return Home", message: "Are you sure???", preferredStyle: .Alert) 

    let alertAction1 = UIAlertAction(title: "Cancel", style: .Default) { (action) in 

    } 
    let alertAction2 = UIAlertAction(title: "Go Home", style: .Destructive) { (action) in 

     let nv = self.storyboard!.instantiateViewControllerWithIdentifier("storyboardidentifier") as! ViewController 
     self.presentViewController(nv, animated:true, completion:nil) 
    } 


    alertController?.addAction(alertAction2) 
    alertController?.addAction(alertAction1) 

    self.presentViewController(alertController!, animated: true, completion: nil) 
    //end alert 

} 
+0

你能告訴我異常消息?在堆棧跟蹤之前,通常會有一條消息跟在異常之後。 – Sweeper

回答

-1

創建的兩個視圖控制器之間的故事情節進行segue,並給它一個識別碼,然後,對警報動作的完成處理程序,您可以觸發SEGUE。一些沿線:

let alertAction2 = UIAlertAction(title: "Go Home", style: .Destructive) { (action) in 
    self.performSegueWithIdentifier("MyStoryboardSegue", sender: self) 
} 

祝你好運!

0

由於您沒有提供太多信息,因此存在很多可能性。我會盡力涵蓋其中大部分。

假設您在BViewController中顯示警報。你想回到AViewController

如果AViewControllerBViewController嵌入在UINavgationController,你將不得不連接BViewControllerAViewController之間的開卷SEGUE。

首先,在AViewController,添加這個方法:從BViewController

@IBAction func unwind(segue: UIStoryBoardSegue) { 

} 

在故事板,控制拖動到AviewController的`「退出」,然後選擇「放鬆:」又名方法,你只是聲明。這是你應該看到

enter image description here

現在給您剛纔添加的標識符SEGUE,說「unwindToHome」。

UIAlertAction再打封閉,啓動SEGUE:

self.performSegueWithIdentifier("unwindToHome", sender: self) 

就是這樣!

如果您呈現BViewController模態,那麼事情比較簡單,只需要調用dismissViewControllerAnimated

self.dismissViewControllerAnimated(true, completion: nil) 
相關問題