I am an android developer.
I want to use custom UIAlertViewDelegate
in my code.
demo like
import UIKit
class ViewController: UIViewController,UIAlertViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
println(buttonIndex)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onClick1(sender: UIButton) {
// define here not work
let delegate = MyUIAlertViewDelegate()
let alert = UIAlertView(title: "title", message: "message", delegate: delegate, cancelButtonTitle: "cancel", otherButtonTitles: "other","hello")
alert.alertViewStyle = UIAlertViewStyle.Default
alert.show()
}
}
// define here works
// let delegate = MyUIAlertViewDelegate()
class MyUIAlertViewDelegate : UIViewController, UIAlertViewDelegate {
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){
println("click" + String(buttonIndex))
}
}
but it not work. why?
I noticed when you are creating a UIAlertView called "alert" you set message to message, for demo purposes this wont work unless message is a variable with a string value.
Also you are calling alert.show() in nothing! it needs to be called by something or somewhere, so in the example I have done below it is in viewDidAppear.
Please copy and paste this code if you need to it works perfectly, and please don't hesitate to ask any questions.