swift自定义UIAlertViewDelegate不起作用

时间:2015-06-10 05:58:01

标签: swift delegates

我是一名Android开发人员。

我想在我的代码中使用自定义UIAlertViewDelegate

演示

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

但它不起作用。 为什么呢?

3 个答案:

答案 0 :(得分:1)

当你创建一个名为" alert"的UIAlertView时,我注意到了。您将消息设置为消息,出于演示目的,除非消息是具有字符串值的变量,否则此操作不会起作用。

你也在呼唤alert.show()!它需要被某个东西或某个地方调用,所以在我下面的例子中它是在viewDidAppear中。

如果您需要将此代码完美运行,请复制并粘贴此代码,请不要犹豫,提出任何问题。

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")

答案 1 :(得分:0)

试试这个

子类作为viewcontroller,而不是nsobject

       class ViewController: UIViewController ,UIAlertViewDelegate{
             }

在故事板中添加按钮或创建自己。然后 在按钮操作中添加给定的代码。

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

    alert.alertViewStyle = UIAlertViewStyle.Default

    alert.show()

然后

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

    if buttonIndex == 0 {

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

您可以根据需要进行自定义 希望它有所帮助

答案 2 :(得分:0)

我实际上已多次使用以下内容,您也可以尝试这样做。

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)


}