我想添加“确定”按钮以外的其他按钮,该按钮应该只是关闭警报.
我想要另一个按钮来调用某个功能.
我想要另一个按钮来调用某个功能.
var logInErrorAlert: UIAlertView = UIAlertView() logInErrorAlert.title = "Ooops" logInErrorAlert.message = "Unable to log in." logInErrorAlert.addButtonWithTitle("Ok")
如何在此警报中添加另一个按钮,然后允许它在点击后调用一个函数,让我们说我们想要调用新按钮:
retry()
Swifty方式是使用新的UIAlertController和闭包:
// 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)
斯威夫特3:
// Create the alert controller let alertController = UIAlertController(title: "Title",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,completion: nil)