Ios Swift消息框

Ios Swift消息框,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,如何在Swift中添加消息框并使其与IOS 7兼容+ 我有以下适用于IOS 8的代码: var alert = UIAlertController(title: "hello world", message: "DO YOU WANT TO PLAY", preferredStyle: UIAlertControllerStyle.Alert) self.presentViewController(alert, animated: false, completion:

如何在Swift中添加消息框并使其与IOS 7兼容+

我有以下适用于IOS 8的代码:

 var alert = UIAlertController(title: "hello world", message:
            "DO YOU WANT TO PLAY", preferredStyle: UIAlertControllerStyle.Alert)

self.presentViewController(alert, animated: false, completion: nil)

alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default,
      handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler:
  {action in      
   // CODE
}))
我有以下与IOS 7+一起使用的代码(我不知道如何响应按钮点击):


如何修改这些代码并使其完全工作?

您可以使类符合“UIAlertViewDelegate”协议并实现此方法:

func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {

    if (buttonIndex == 0) {
       //Do something
    }
}
在那里,您可以处理与alertview的任何用户交互。 通过检查buttonIndex变量,可以确定用户点击了哪个按钮


这将在iOS 7和iOS 8中起作用,iOS 7中的UIAlerView和iOS 8中的UIAlertController都起作用。编写您的版本,如果它不起作用,请返回并获得帮助。因此,这不是一个代码编写服务,我们将帮助您编写代码。
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {

    if (buttonIndex == 0) {
       //Do something
    }
}