问题描述
我有一个带有选项按钮的警报控制器,可以在文本字段中设置一个值,然后在完成时调用一个方法。警报视图会正常弹出,但是当按下按钮设置文本字段值时,该方法将在文本字段中设置值之前先运行,然后实际运行。我在这里尝试了一些建议,例如使用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的处理程序中添加[弱自我]?