2016-08-03 23 views
2
//Event created alert 
let alert = UIAlertController(title: "Event Created", message: "Event successfully created", preferredStyle: UIAlertControllerStyle.Alert); 
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil)); 
self.presentViewController(alert, animated: true, completion: nil); 

//Pop back to table 
self.navigationController!.popToRootViewControllerAnimated(true); 

在此代碼中,我創建了一個警報,並直接在警報之後使用popToRootViewControllerAnimated方法。這不起作用的原因,我發現的解決方法是調用presentViewController完成內部的方法。爲什麼popviewcontroller在alertview之後的presentviewcontroller之後工作?

爲什麼pop方法在presentViewController方法之後不起作用,除非將其放入閉包中?

回答

1

當您致電self.presentViewController(alert, animated: true, completion: nil)時,viewController將執行演示。而且當它進展時,你不能執行任何其他轉換/演示。

其實,我的機器上運行代碼時,我得到這個日誌:

popToViewController:轉型:呼籲,而現有的過渡或演示文稿正在發生;導航堆棧將不會更新。

這應該說明自己很清楚。您應該將此行self.navigationController!.popToRootViewControllerAnimated(true)改爲完成關閉。

3

我想你想只拍了拍OK後用戶使用popToRoot,在這種情況下:

// Created the alert 
let alert = UIAlertController(title: "Event Created", message: "Event successfully created", preferredStyle: UIAlertControllerStyle.Alert) 

// Create the action 
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action: UIAlertAction!) -> Void in 
    self.navigationController!.popToRootViewControllerAnimated(true); 
    } 
// Add the action 
alert.addAction(OKAction) 

// Present the alert 
self.presentViewController(alert, animated: true, completion: nil) 

注:

  • 你並不真的需要迅速使用;
相關問題