UIAlertController addAction不起作用

时间:2016-03-21 18:34:51

标签: ios swift

因此UIAlertController正确地在屏幕上显示警报,但是在xCode模拟器中按下确定没有任何操作,它也不会保留警报。 alert view image

    func displayAlertMessage(userMessage:String) {

    let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil);

    myAlert.addAction(okAction);

    self.presentViewController(myAlert, animated: true, completion: nil);

}

1 个答案:

答案 0 :(得分:1)

将UIAlertActionStyle.Default更改为UIAlertActionStyle.Cancel

func displayAlertMessage(userMessage:String) {

let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);

let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: nil);

myAlert.addAction(okAction);

self.presentViewController(myAlert, animated: true, completion: nil);

}

如果你想做好事

 let okAction = UIAlertAction(title: "OK", style: .Default) { (action) in
      //do whatever you want to do here
     // for e.g dismiss view controller or segue your view controller etc.
    }