【问题标题】:Unusual behavior with UIAlertViewControllerUIAlertViewController 的异常行为
【发布时间】:2017-05-11 16:11:49
【问题描述】:

我有一个 UIAlertViewController 的简单用法,其中我将按钮标签和操作函数作为参数传递给名为 showAlert() 的函数,该函数设置并调用警报。我在 in 闭包的 UIAlertAction 中有操作函数。

异常行为:当 showAlert 被执行而不是按钮被按下时,action 函数就会被执行。

另一个问题是当按下按钮时警报会自动消失。我没有解雇声明。

    class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func test1() {
        NSLog("test 1 executed")
    }




    @IBAction func show(_ sender: UIButton) {
        self.showAlert(title: "Title", message: "Message",titleString: "A,B,C", function:test1())
    }
    func showAlert(title: String, message: String, titleString: String, function: ()) {
        let cancelButtonTitle = NSLocalizedString("Cancel", comment: "")
        let labels = titleString.components(separatedBy: ",")
        var actions = [UIAlertAction()]
        for label in labels {
            let a = UIAlertAction(title: label, style: .default) { action in
                function // executed as soon as showAlert is called!! 
                         // Expecting to be called when button is pressed
                NSLog("\(label) Pressed")

            }
            actions.append(a)
        }

        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

        // Create the actions.
        let cancelAction = UIAlertAction(title: cancelButtonTitle, style: .cancel) { action in
            NSLog("Cancel Button pressed.")
        }
        for action in actions {
            alertController.addAction(action)
        }
        // Add the actions.
        alertController.addAction(cancelAction)

        present(alertController, animated: true, completion: nil)
    }

}

控制台上的响应是: 2017-05-11 11:55:15.593 AlertTest[5304:8818290] 函数执行 2017-05-11 11:55:25.287 AlertTest[5304:8818290] 按下

【问题讨论】:

    标签: swift3 uialertcontroller


    【解决方案1】:

    记住这一点:

    在方法/函数名称的末尾添加 () 将调用该方法。删除 () 以“引用”该方法/函数。

    当你这样做时:

    //                                                                           I mean this
    //                                                                                 |
    //                                                                                 v 
    self.showAlert(title: "Title", message: "Message",titleString: "A,B,C", function:test1())
    

    您正在调用test 并将其返回值用作showAlert 的参数。

    当到达该行时,首先调用test() 以评估其返回值,然后调用showAlert,从而得到控制台输出。

    要引用方法test,请删除()s。

    self.showAlert(title: "Title", message: "Message",titleString: "A,B,C", function:test1)
    

    你也写错了参数类型。不带参数不返回什么的函数类型应该是() -> Void

    func showAlert(title: String, message: String, 
        titleString: String, function: () -> Void) {
    

    此外,在警报闭包中,将 () 添加到函数中,因为您现在正在尝试调用它:

    let a = UIAlertAction(title: label, style: .default) { action in
        function() // Note the ()s
        NSLog("\(label) Pressed")
    
    }
    

    附:我不认为让所有按钮都做同样的事情是你真正想要的。不过我可能错了。

    【讨论】:

    • 效果很好。感谢您的回复和教育。不,我需要传递一系列函数,但我仍在为正确的格式而苦苦挣扎。阅读马特·纽伯格的书。我真的需要阅读闭包。
    • @SyedTariq 如果要传递函数数组,请将[() -> Void] 作为参数类型。但我个人认为最好去掉整个showAlert函数,手动添加UIAlertActions。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2018-03-14
    • 2020-05-02
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多