xcode - 带有确定和取消按钮的快捷警报视图(iOS8),点击该按钮

我在用Swift编写的Xcode中有一个警报视图,我想确定用户选择哪个按钮(它是一个确认对话框)什么都不做或者执行某些操作。目前我有:

@IBAction func pushedRefresh(sender: AnyObject) {
        var refreshAlert = UIAlertView()
        refreshAlert.title = "Refresh?"
        refreshAlert.message = "All data will be lost."
        refreshAlert.addButtonWithTitle("Cancel")
        refreshAlert.addButtonWithTitle("OK")
        refreshAlert.show()
    }

我可能使用错误的按钮,请更正我,因为这对我来说都是新的。

谢谢!

B_s asked 2019-08-13T23:50:09Z
4个解决方案
280 votes

如果您使用的是iOS8,则应使用UIAlertController - 不推荐使用UIAlertView。

以下是如何使用它的示例:

var refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
  print("Handle Ok logic here")
  }))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
  print("Handle Cancel Logic here")
  }))

presentViewController(refreshAlert, animated: true, completion: nil)

正如您所看到的,UIAlertAction的块处理程序处理按钮按下。 这里有一个很棒的教程(尽管本教程不是使用swift编写的):[http://hayageek.com/uialertcontroller-example-ios/]

Swift 3更新:

let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertControllerStyle.alert)

refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
    print("Handle Ok logic here")
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
    print("Handle Cancel Logic here")
}))

present(refreshAlert, animated: true, completion: nil)
Michael Wildermuth answered 2019-08-13T23:50:37Z
17 votes
var refreshAlert = UIAlertController(title: "Log Out", message: "Are You Sure to Log Out ? ", preferredStyle: UIAlertControllerStyle.Alert)

refreshAlert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
    self.navigationController?.popToRootViewControllerAnimated(true)
}))

refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in

    refreshAlert .dismissViewControllerAnimated(true, completion: nil)


}))

presentViewController(refreshAlert, animated: true, completion: nil)
A.G answered 2019-08-13T23:50:54Z
3 votes

您可以使用UIAlertController轻松完成此操作

let alertController = UIAlertController(
       title: "Your title", message: "Your message", preferredStyle: .alert)
let defaultAction = UIAlertAction(
       title: "Close Alert", style: .default, handler: nil)
//you can add custom actions as well 
alertController.addAction(defaultAction)

present(alertController, animated: true, completion: nil)

参考:iOS Show Alert

Shaba Aafreen answered 2019-08-13T23:51:27Z
2 votes

针对swift 3进行了更新:

//函数定义:

@IBAction func showAlertDialog(_ sender: UIButton) {
        // Declare Alert
        let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to Logout?", preferredStyle: .alert)

        // Create OK button with action handler
        let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
             print("Ok button click...")
             self.logoutFun()
        })

        // Create Cancel button with action handlder
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
            print("Cancel button click...")
        }

        //Add OK and Cancel button to dialog message
        dialogMessage.addAction(ok)
        dialogMessage.addAction(cancel)

        // Present dialog message to user
        self.present(dialogMessage, animated: true, completion: nil)
    }

// logout Fun()函数定义:

func logoutFun()
{
    print("Logout Successfully...!")
}
Kiran jadhav answered 2019-08-13T23:52:05Z
translate from https://stackoverflow.com:/questions/25511945/swift-alert-view-ios8-with-ok-and-cancel-button-which-button-tapped