Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift IOS UILabel不';在重新加载应用程序或单击其他位置之前,我不会更新_Swift_Uialertcontroller - Fatal编程技术网

Swift IOS UILabel不';在重新加载应用程序或单击其他位置之前,我不会更新

Swift IOS UILabel不';在重新加载应用程序或单击其他位置之前,我不会更新,swift,uialertcontroller,Swift,Uialertcontroller,我不熟悉iOS开发,也不熟悉闭包的概念。使用故事板,我创建了一个ui按钮和ui标签,因此当我单击按钮时,它会显示一个带有动作的警报,当我解除警报时,标签会增加标签内的数字,但我希望该增量在警报解除之后,因此我使用了关闭的概念,以便可以延迟警报的增加标签内的数字如下: class ViewController: UIViewController { @IBOutlet weak var numberLbl: UILabel! var count = 1 func upda

我不熟悉iOS开发,也不熟悉闭包的概念。使用故事板,我创建了一个
ui按钮
ui标签
,因此当我单击按钮时,它会显示一个带有动作的警报,当我解除警报时,标签会增加标签内的数字,但我希望该增量在警报解除之后,因此我使用了关闭的概念,以便可以延迟警报的增加标签内的数字如下:

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年末,我正在使用MacBookPro


谢谢

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

@IBAction func btnPressed(_ sender: UIButton)
除此之外,您可能希望尝试一个干净的(命令Shift K)并尝试重建


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

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

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

func updateLabels() {    
    count += 1
}

经过长时间的搜索,我发现这是因为Mac电脑中有一个英特尔HD graphic 3000的错误,所以这是一个驱动程序错误,解决方法是执行这个命令,它确实对我有效(谢天谢地)

或者根据您的硬件使用1


谢谢你的帮助:)

谢谢你的回答,是的,我完全忘记了争论“(\usnder:UIButton)”。但我的问题是,在我解除警报后,标签不会自动更新(增加数字),但在我触摸侧面音量按钮后,标签会增加,就像它挂在那里等待什么一样???说实话,这听起来像是一个模拟器的错误。你试过在一台设备上测试吗?是的,在几次尝试使用模拟器失败后,我确实在一台物理设备上测试了它,它工作得很好,但在模拟器上,它有一种奇怪的行为,我必须触摸侧面的音量按钮来强制更新标签或整个UIView,并且它显示了正确的数字!当我创建numberlabel(UILabel)时,我甚至会去掉
UpdateLabel
函数,直接增加值所以它已经有了0,并用1初始化了计数,在计数增加之前,它会将UILabel更新1,然后为下一次调用计数增加,但问题是UI挂起,我不得不触摸音量侧按钮,UILabel突然增加。从下面的评论来看,我认为您的计算机太旧太慢了。嘿,经过长时间的搜索,我发现这是因为Mac电脑中有一个英特尔HD graphic 3000的错误,所以这是一个驱动程序错误,解决方法是执行这个命令,这个命令对我很有效(谢天谢地):“defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferEmulationHint 2”或使用1,具体取决于您的硬件
defaults write com.apple.CoreSimulator.IndigoFramebufferServices FramebufferEmulationHint 2