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

IOS UILabel doesn't update until i reload the app or clicking elsewhere

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

但发生的事情是,每当我 运行 模拟器并单击按钮时,警报就会出现,然后单击操作将其关闭,但标签不会增加,直到我触摸模拟器上的某个侧面按钮或者我在它递增后重新加载应用程序

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

我使用的是 2011 年底的 macbook pro

谢谢

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

@IBAction func btnPressed(_ sender: UIButton)

除此之外,您可能想尝试清理(Command Shift K)并尝试重建。

除此之外,我 运行 你的代码是原样的,它工作正常。

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

var count : Int = 1 {
    didSet {
        numberLbl.text = "\(count)"
    }
}

func updateLabels() {    
    count += 1
}

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

defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferEmulationHint 2

或根据您的硬件使用 1

谢谢你的帮助:)