2015-11-13 20 views
0

當有人對警報操作說好時,我想導航回到根視圖控制器。但警報操作不允許訪問自我。如何從Swift中的UIAlertAction訪問自我?

什麼是圍繞工作AlertAction來獲取當前導航控制器,

這裏是代碼,

func buttonAction(sender:UIButton!) 
{ 
let alertController = UIAlertController(title: "IQ", message:"Thank you for your feedback!", preferredStyle: .Alert) 
alertController.addAction(okAction) 
self.presentViewController(alertController, animated: true, completion: nil) 
} 

var okAction = UIAlertAction(title: "Menu", style:UIAlertActionStyle.Default) { 
UIAlertAction in 
NSLog("OK Pressed") 
self.navigationController?.popToRootViewControllerAnimated(true) //error 
} 

回答

0

你可以做到這一點,而不是這樣:

let alert = UIAlertController(title: "Alert", message: "Message.", preferredStyle: UIAlertControllerStyle.Alert) 
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { action in 
     self.navigationController?.popToRootViewControllerAnimated(true) 
})) 
self.presentViewController(alert, animated: true, completion: nil) 
0

您需要在嘗試從導航控制器彈出之前解除所呈現的視圖控制器(警報)。

1

您所要做的就是爲okAction屬性定義一個吸氣劑。

var okAction: UIAlertAction {   
    get { 
     return UIAlertAction(title: "Menu", style:UIAlertActionStyle.Default) { 
      UIAlertAction in 
      NSLog("OK Pressed") 
      self.navigationController?.popToRootViewControllerAnimated(true) 
     } 
    } 
} 

測試在Xcode 7.1.1

相關問題