我需要使用AlertView
,因为它还与 iOS7 兼容。
我尝试在UITableViewController
:
func alertView(View: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
println("in alertView")
switch buttonIndex {
case 1: println("1")
case 0: println("2")
default: println("nil")
}
}
@IBAction func entryPoint(sender: AnyObject) {
var alert = UIAlertView()
alert.delegate = self
println("Pressed")
alert.title = "Enter Input"
alert.addButtonWithTitle("Done")
alert.addButtonWithTitle("Cancel")
alert.alertViewStyle = UIAlertViewStyle.PlainTextInput
var text = alert.textFieldAtIndex(0)
text?.placeholder = "inserisci"
println(text?.text)
alert.show()
}
但似乎他不能打电话给他的代表(func alertView()
)。
当我尝试启动应用程序时:
正确显示AlertView
,但当按下按钮时会发生以下情况:
"完成" - 什么都没发生
"取消" - 在日志中写了nil
感谢。
答案 0 :(得分:0)
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(查看:UIAlertView!,clickedButtonAtIndex buttonIndex:Int){
switch buttonIndex{
case 1:
NSLog("Retry");
break;
case 0:
NSLog("Dismiss");
break;
default:
NSLog("Default");
break;
//Some code here..
}
}
点击“关闭”按钮时打印关闭。