AlertAction在完成Swift 5中延迟

问题描述

我有一个带有选项按钮的警报控制器,可以在文本字段中设置一个值,然后在完成时调用一个方法。警报视图会正常弹出,但是当按下按钮设置文本字段值时,该方法将在文本字段中设置值之前先运行,然后实际运行。我在这里尝试了一些建议,例如使用dispatchQueue.main.async,但没有区别。有什么方法可以延迟方法调用,以便可以在调用方法之前设置文本字段值,还是可以加快在文本字段中设置值的方法?还是我错过了代码中的某些内容

//Alert message function
func alertMessage(message: String,changeLDW: Int,changeZFW: Int) {
    
    let attributedString = NSAttributedString(string: "WARNING!",attributes: [
        NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20),NSAttributedString.Key.foregroundColor : UIColor.red ])
    
    AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
    let alertController = UIAlertController(title: "",message: message,preferredStyle: .alert)
    alertController.setValue(attributedString,forKey: "attributedTitle")
    let cancelAction = UIAlertAction(title: "Ignore",style: .destructive,handler: nil)
    let setLDWAction = UIAlertAction(title: "Set LDW",style: .default) { (_) -> Void in
        self.maxLDWTextField.text = String(changeLDW)
        self.calculateEstimates()
        }
    let setZFWAction = UIAlertAction(title: "Set ZFW",style: .cancel)  { (_) -> Void in
        self.estimatedZFWTextField.text = String(changeZFW)
        self.calculateEstimates()
        }
    
    if changeLDW > 0 && changeZFW > 0{
        alertController.addAction(cancelAction)
        alertController.addAction(setLDWAction)
        alertController.addAction(setZFWAction)
    }
    else if changeZFW > 0 && changeLDW == 0 {
        alertController.addAction(cancelAction)
        alertController.addAction(setZFWAction)
    }
    else if changeLDW > 0 && changeZFW == 0 {
        alertController.addAction(cancelAction)
        alertController.addAction(setLDWAction)
    }
    else {
        alertController.addAction(cancelAction)
    }
    self.present(alertController,animated: true,completion: nil)
}

解决方法

为什么不在UIAlertAction的处理程序中添加[弱自我]?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...