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

前端 未结 6 793
耶瑟儿~
耶瑟儿~ 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 02:02

    You can easily do this by using 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)
    

    .

    Reference: iOS Show Alert

提交回复
热议问题