[英]How to add action to UIAlertView in Swift (iOS 7)
我想在重试按钮中添加一个动作,但是当用户按下重试按钮时没有任何反应。
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:
createAccount()
default:
//Some code here..
}
}
我测试了你的代码,它对我来说很好,它打印相应的结果:
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..
}
}
当我点击关闭按钮时打印关闭。
那是因为你的重试按钮的索引是1,而不是0. dismiss按钮的索引为0。
如果将以下代码放在操场中,您将实际看到按钮的索引(如addButtonWithTitle方法中所述):
var createAccountErrorAlert: UIAlertView = UIAlertView()
createAccountErrorAlert.title = "Oops"
createAccountErrorAlert.message = "Could not create account!"
createAccountErrorAlert.addButtonWithTitle("Dismiss") //Prints 0
createAccountErrorAlert.addButtonWithTitle("Retry") //Prints 1
索引错了。 以下(基于iOS单视图默认应用程序模板)适用于我(我连接按钮以触发@IBAction buttonPressed
)。 不要忘记让您的视图控制器成为UIAlertViewDelegate
(虽然以下工作没有这样做):
import UIKit
class ViewController: UIViewController, UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(sender : AnyObject) {
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 {
default:
println("alertView \(buttonIndex) clicked")
}
}
}
继续我的方式,使用较短的代码行:
func showAlert(){
var alert = UIAlertView(title: "Choose", message: "Which_pill", delegate: self, cancelButtonTitle:"Red")
alert.addButtonWithTitle("Blue")
alert.show()
}
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){
switch buttonIndex{
case 1: println("Blue")
case 0: println("Red")
default: println("Is this part even possible?")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.