0

当有人对警报操作说好的时,我想导航回根视图控制器。但是警报操作不允许访问自己。

在 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
}
4

3 回答 3

1

您所要做的就是为该okAction属性定义一个 getter。

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

在 Xcode 7.1.1 中测试

于 2015-11-13T23:16:38.950 回答
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)
于 2015-11-13T23:12:20.473 回答
0

在尝试从导航控制器弹出之前,您需要关闭呈现的视图控制器(警报)。

于 2015-11-13T23:15:45.980 回答