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

后端 未结 8 1761
说谎
说谎 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 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
    

提交回复
热议问题