IOS UILabel在我重新加载应用程序或单击其他地方之前不会更新



我是iOS开发的新手,也是闭包概念的新手。使用情节提要,我创建了一个UIButtonUILabel因此当我单击按钮时,它会显示一个带有操作的警报,当我关闭警报时,标签会增加标签内的数字,但我希望该增量是在警报消除之后,所以我使用了闭包的概念,所以我可以延迟标签内数字的增量,如下所示:

class ViewController: UIViewController {
@IBOutlet weak var numberLbl: UILabel!
var count = 1
func updateLabels(){
numberLbl.text = String(count)
count += 1
}
@IBAction func btnPressed(){
let alert = UIAlertController(title: "hello", message: "this is an alert", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { action in
print("incrementing.... ") // it print this text
// in console
self.updateLabels()        // but not this line does not work
// but when i reload the app it updates the number label
/// !!!!!!!!
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}

但是发生的情况是,当我运行模拟器并单击按钮时,警报会显示并单击操作以关闭它,但标签不会增加,直到我触摸模拟器上的某个侧面按钮或我重新加载应用程序之后它才会增加

我在物理设备上尝试过,我工作正常,但在模拟器上它没有

我使用的是 MacBook Pro 2011 年末

谢谢

您在更新计数变量之前更新了 UILabel 文本

var count : Int = 1 {
didSet {
numberLbl.text = "(count)"
}
}
func updateLabels() {    
count += 1
}

经过长时间的搜索,我发现这是因为带有英特尔高清显卡 3000 的 Mac 中的一个错误,所以这是一个驱动程序错误,解决方法是执行这个对我有用的命令(谢天谢地(

defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferEmulationHint 2

或使用 1,具体取决于您的硬件

谢谢你的帮助:)

我在代码中更改的代码的唯一方面是您声明按钮的方式。

@IBAction func btnPressed(_ sender: UIButton)

除此之外,您可能还想尝试干净(命令移位 K(并尝试重建。

除此之外,我按原样运行您的代码并且它工作正常。

最新更新