How to add an action to a UIAlertView button using Swift iOS

后端 未结 8 1641
说谎
说谎 2020-12-12 22:13

I want to add another button other than the \"OK\" button which should just dismiss the alert. I want the other button to call a certain function.

var logInE         


        
相关标签:
8条回答
  • 2020-12-12 22:40

    Swift 4 Update

            // Create the alert controller
            let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    
            // Create the actions
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }
            let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                UIAlertAction in
                NSLog("Cancel Pressed")
            }
    
            // Add the actions
            alertController.addAction(okAction)
            alertController.addAction(cancelAction)
    
            // Present the controller
            self.present(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-12 22:43

    this is for swift 4.2, 5 and 5+

    let alert = UIAlertController(title: "ooops!", message: "Unable to login", preferredStyle: .alert)
    
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
    
    self.present(alert, animated: true)
    
    0 讨论(0)
  • 2020-12-12 22:53

    Swift 3.0 Version of Jake's Answer

    // Create the alert controller

    let alertController = UIAlertController(title: "Alert!", message: "There is no items for the current user", preferredStyle: .alert)
    
                // Create the actions
                let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
                    UIAlertAction in
                    NSLog("OK Pressed")
                }
                let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
                    UIAlertAction in
                    NSLog("Cancel Pressed")
                }
    
                // Add the actions
                alertController.addAction(okAction)
                alertController.addAction(cancelAction)
    
                // Present the controller
                self.present(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-12 22:54
    func showAlertAction(title: String, message: String){
        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: {(action:UIAlertAction!) in
            print("Action")
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
    
    0 讨论(0)
  • 2020-12-12 22:58

    The Swifty way is to use the new UIAlertController and closures:

        // Create the alert controller
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    
        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }
    
        // Add the actions
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
    
        // Present the controller
        self.presentViewController(alertController, animated: true, completion: nil)
    

    Swift 3:

        // Create the alert controller
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
    
        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
            UIAlertAction in
            NSLog("Cancel Pressed")
        }
    
        // Add the actions
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
    
        // Present the controller
        self.present(alertController, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-12 23:04

    UIAlertViews use a delegate to communicate with you, the client.

    You add a second button, and you create an object to receive the delegate messages from the view:

    class LogInErrorDelegate : UIAlertViewDelegate {
    
        init {}
    
        // not sure of the prototype of this, you should look it up
        func alertView(view :UIAlertView, clickedButtonAtIndex :Integer) -> Void {
            switch clickedButtonAtIndex {
                case 0: 
                   userClickedOK() // er something
                case 1:
                   userClickedRetry()
                  /* Don't use "retry" as a function name, it's a reserved word */
    
                default:
                   userClickedRetry()
            }
        }
    
        /* implement rest of the delegate */
    }
    logInErrorAlert.addButtonWithTitle("Retry")
    
    var myErrorDelegate = LogInErrorDelegate()
    logInErrorAlert.delegate = myErrorDelegate
    
    0 讨论(0)
提交回复
热议问题