Swift alert view with OK and Cancel: which button tapped?

前端 未结 6 788
耶瑟儿~
耶瑟儿~ 2021-01-30 01:16

I have an alert view in Xcode written in Swift and I\'d like to determine which button the user selected (it is a confirmation dialog) to do nothing or to execute something.

6条回答
  •  暖寄归人
    2021-01-30 01:49

    small update for swift 5:

    let refreshAlert = UIAlertController(title: "Refresh", message: "All data will be lost.", preferredStyle: UIAlertController.Style.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")
        }))
    
        self.present(refreshAlert, animated: true, completion: nil)
    

提交回复
热议问题