gpt4 book ai didi

ios - Swift - 即使控件可见, View 也不显示/更新控件

转载 作者:行者123 更新时间:2023-11-30 14:14:44 27 4
gpt4 key购买 nike

对这个可怕的标题表示歉意,但我真的想不出更好的方式来表达它。我的代码如下:

@IBAction func btnSaveTapped(sender: UIButton) {
println("Save Button Pressed")
self.StartAI()
self.btnSave.enabled = false
self.AnimateButton(sender)

var booContinueWithSend:Bool = true
(Continues)

当我运行上面的代码时,屏幕上的控件没有变化。我已经单步执行了代码,每一行都没有变化。即使对于 btnSave.enabled 也没有任何变化。我已通过控制台调试器确认该按钮未启用,但它仍然显示为启用状态。是的,我已将其设置为根据状态明显改变(代码如下):

self.btnSave.setTitleColor(conButtonTextColour, forState: UIControlState.Normal)
self.btnSave.setTitleColor(conButtonTextColourDisabled, forState: UIControlState.Disabled)

其中 conButtonTextColourUIColor.blackColorconButtonTextColourDisabled 为灰色。

我的事件指示器也会发生这种情况,我已成功手动测试了该指示器。当我运行上面的代码(btnSaveTapped)时,没有任何反应(它没有像应有的那样显示)。我在这里以编程方式创建人工智能:

func CreateAI() {
let conAIViewHeightWidth:CGFloat = 100
let conHeightWidth:CGFloat = 50

self.aiView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(self.aiView)
self.aiView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
self.aiView.layer.cornerRadius = 10

var constraintViewHeight:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: conAIViewHeightWidth)
var constraintViewWidth:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: conAIViewHeightWidth)
self.aiView.addConstraints([constraintViewHeight, constraintViewWidth])

var constraintViewX:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
var constraintViewY:NSLayoutConstraint = NSLayoutConstraint(item: self.aiView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.view.addConstraints([constraintViewX, constraintViewY])


self.aiActivityIndicator.setTranslatesAutoresizingMaskIntoConstraints(false)
self.aiView.addSubview(self.aiActivityIndicator)
self.aiActivityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge


var constraintX:NSLayoutConstraint = NSLayoutConstraint(item: self.aiActivityIndicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.aiView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
var constraintY:NSLayoutConstraint = NSLayoutConstraint(item: self.aiActivityIndicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.aiView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
self.view.addConstraints([constraintX, constraintY])

self.aiActivityIndicator.hidesWhenStopped = true
self.aiActivityIndicator.startAnimating()

StopAI()
}

这是我分别启动和停止人工智能的两个例程。因为 AI 是 aiView 的 subview ,所以我只是隐藏或取消隐藏它。然而,面对这些问题,我决定变得 super 安全,并确保我取消隐藏并启用人工智能。

func StartAI() {
self.aiView.hidden = false
self.aiActivityIndicator.hidden = false
self.aiActivityIndicator.startAnimating()
println("Started AI")
}

func StopAI() {
self.aiView.hidden = true
println("Stopped AI")
}

我做错了什么?提前致谢。

最佳答案

试试这个代码:

view.layoutIfNeeded()

完成所有更改后实现此操作

关于ios - Swift - 即使控件可见, View 也不显示/更新控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31332837/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com