本文介绍了如何在Swift(iOS 7)中向UIAlertView添加操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在重试按钮中添加一个动作,但是当用户按下重试按钮时没有任何反应。
I want to add an action to the Retry button, but when the user presses the Retry button nothing happens.
var createAccountErrorAlert: UIAlertView = UIAlertView()
createAccountErrorAlert.delegate = self
createAccountErrorAlert.title = "Oops"
createAccountErrorAlert.message = "Could not create account!"
createAccountErrorAlert.addButtonWithTitle("Dismiss")
createAccountErrorAlert.addButtonWithTitle("Retry")
createAccountErrorAlert.show()
确定按下按钮的索引的功能?
Function for determining index of button pressed?
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
switch buttonIndex{
case 1:
createAccount()
default:
//Some code here..
}
}
推荐答案
我测试了你的代码,它对我来说工作正常,打印出相应的结果:
I tested your code and it is working fine for me it prints the respective result:
func showAlert(){
var createAccountErrorAlert: UIAlertView = UIAlertView()
createAccountErrorAlert.delegate = self
createAccountErrorAlert.title = "Oops"
createAccountErrorAlert.message = "Could not create account!"
createAccountErrorAlert.addButtonWithTitle("Dismiss")
createAccountErrorAlert.addButtonWithTitle("Retry")
createAccountErrorAlert.show()
}
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
switch buttonIndex{
case 1:
NSLog("Retry");
break;
case 0:
NSLog("Dismiss");
break;
default:
NSLog("Default");
break;
//Some code here..
}
}
当我点击关闭按钮时打印关闭。
It print dismiss when i click on dismiss button.
这篇关于如何在Swift(iOS 7)中向UIAlertView添加操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!