ios - 在 viewDidLoad 中快速设置属性不起作用,但在 viewDidAppear 中有效
<p><p>最新的 XCode。我有一个项目,其中在任何 Storyboard / ViewController 中设置属性,例如 <code>UILabel</code> 的 <code>textColor</code> 或 <code>UIButton< 的 <code>backgroundColor</code> <code>viewDidLoad</code> 中的/code> 不起作用,但在 <code>viewDidAppear</code> 中起作用。</p>
<p>让我感到沮丧的是,我无法在一个新的/不同的简单项目中重现它。</p>
<p>有人知道可能出了什么问题吗?这是一个大工程。重新创建它将是一个大麻烦。
我有一种感觉,这是无法解决的。请证明我错了。</p>
<p>抱歉,代码不多了:</p>
<pre><code>@IBOutlet weak var theTextField: UITextField!
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var theButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
theTextField.text = "some text" //<<<- works
theTextField.textColor = UIColor.red //<<<- works
theLabel.textColor = UIColor.red //<<<- **doesn't work**
theButton.backgroundColor = UIColor.red //<<<- **doesn't work**
theButton.setTitleColor(UIColor.blue, for: UIControl.State.normal) //<<<- works
}
override func **viewDidAppear**(_ animated: Bool) {
super.viewDidAppear(animated)
theTextField.text = "some text" //<<<- works
theTextField.textColor = UIColor.red //<<<- works
theLabel.textColor = UIColor.red //<<<- **works**
theButton.backgroundColor = UIColor.red //<<<- **works**
theButton.setTitleColor(UIColor.blue, for: UIControl.State.normal) //<<<- works
}
</code></pre>
<p>这里 <strong>theTextField</strong> 在所有情况下都可以正常更新,但标签和按钮不会在“viewDidLoad”中更新。</p>
<p><strong>编辑</strong>:更新代码以显示某些东西有效而另一些无效</p>
<p><strong>EDIT 2</strong>:使用 <code>viewDidAppear</code> 的问题是屏幕显示了一组颜色(在设计器中指定),但随后所有颜色都在用户面前切换- 看起来很难看。任何形式的延误都无济于事。目标是在动画进入视野之前用所有颜色等“准备”屏幕。而且这些颜色依赖于一些动态数据,不能在设计器中指定。</p>
<p><strong>EDIT 3</strong>:我只是认为在 <code>viewDidLayoutSubviews</code> 中完成所有这些操作可以得到我想要的:</p>
<pre><code>@IBOutlet weak var theTextField: UITextField!
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var theButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
theTextField.text = "some text" //<<<- works
theTextField.textColor = UIColor.red //<<<- works
theLabel.textColor = UIColor.red //<<<- doesn't work
theButton.backgroundColor = UIColor.red //<<<- doesn't work
theButton.setTitleColor(UIColor.blue, for: UIControl.State.normal) //<<<- works
}
override func viewDidLayoutSubviews() {
super.viewDidAppear()
theTextField.text = "some text" //<<<- works
theTextField.textColor = UIColor.red //<<<- works
theLabel.textColor = UIColor.red //<<<- works
theButton.backgroundColor = UIColor.red //<<<- works
theButton.setTitleColor(UIColor.blue, for: UIControl.State.normal) //<<<- works
}
</code></pre>
<p>似乎是一种可能的解决方法。但为什么?为什么我需要求助于解决方法?我真的很想了解为什么 <code>viewDidLoad</code> 在我的这个特殊案例/项目中不起作用。或者这不是解决方法和正确的方法吗?我在所有示例代码中看到的所有地方,每个人总是在 <code>viewDidLoad</code> 中进行所有此类初始化。我从未见过有人在 <code>viewDidLayoutSubviews</code> 或 <code>viewDidAppear</code> 中这样做。我看的时间还不够长吗?我错过了什么? </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>没有错。它在执行代码之前渲染了标签。试试下面的代码就行了</p>
<pre><code>@IBOutlet weak var theLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
theLabel.textColor = UIColor.red
})
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 viewDidLoad 中快速设置属性不起作用,但在 viewDidAppear 中有效,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/53531996/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/53531996/
</a>
</p>
页:
[1]