swift custom UIAlertViewDelegate not work

460 views Asked by At

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?

3

There are 3 answers

1
Dom Bryan On BEST ANSWER

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.

import UIKit

class ViewController: UIViewController {

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.
}

override func viewDidAppear(animated: Bool) {
    alert.show()
}

}

class MyUIAlertViewDelegate : NSObject, UIAlertViewDelegate {
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    print("click" + String(buttonIndex))
}
}

let delegate = MyUIAlertViewDelegate()
let alert = UIAlertView(title: "warning", message: "This is a message", delegate: delegate, cancelButtonTitle: "Cancel")
4
Lydia On

Try like this

Subclass as viewcontroller ,instead of nsobject

       class ViewController: UIViewController ,UIAlertViewDelegate{
             }

Add a button in storyboard or create yourself.Then in button action add the given code.

       var  alert = UIAlertView(title: " Your Title", message: "your message", delegate: self, cancelButtonTitle: "Ok", otherButtonTitles: "Cancel")

    alert.alertViewStyle = UIAlertViewStyle.Default

    alert.show()

Then

           func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
        println("click",buttonIndex)

    if buttonIndex == 0 {

        println("ok")
    }else {
        println("Cancel")
    }
       }
}

You can customize as you need Hope it helps

0
Gee Kay On

I've been using the following many times actually, you can try this as well.

func displayAlert(title: String, message: String) {

    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction((UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in

        self.dismissViewControllerAnimated(true, completion: nil)

    })))

    self.presentViewController(alert, animated: true, completion: nil)


}