我有一个@IBOutlet连接到UIButton.在我的代码中,我有以下方法,只要用户执行操作就会执行该方法.我已经设法用一些虚拟函数替换我的代码,向你们展示我的问题是什么.
要复制此问题,我使用的是UITableViewController的子类,您可以在下面看到它:
class MyTableViewController : UITableViewController {
@IBOutlet nextOrCreateButton: UIButton!
var dummy = [1, 2]
var index: Int = 0
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dummy.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return tableView.dequeueReusableCellWithIdentifier("TemplateCell") as UITableViewCell!
}
@IBAction func onNextClicked() {
if dummy.count == (index + 1) {
dummy.append(0)
}
index++
}
func updateUI() {
if dummy.count == (index + 1) {
print("Setting text to FOO")
nextOrCreateButton.titleView?.text = "FOO"
if dummy.count == 4 {
print("Setting text to FOOBAR")
nextOrCreateButton.titleView?.text = "FOOBAR"
nextOrCreateButton.enabled = false
}
} else {
print("Setting text to BAR")
nextOrCreateButton.titleView?.text = "BAR"
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码有一个单击的按钮,当它被单击时,它将移动到下一个虚拟值,虚拟值不用于此示例中的任何内容,除了显示更改文本不起作用.
会发生什么是代码执行它应该如何(根据调试消息)但按钮的文本更改为它设置的内容,并几乎立即更改回来.
单击X次按钮可打印以下日志:
Setting text to BAR
Setting text to FOO
Setting text to FOO
Setting text to FOO
Setting text to FOOBAR
Run Code Online (Sandbox Code Playgroud)
但是,按钮总是快速变回FOO,即使在更改为"FOOBAR"之后,按钮也不会像我们在代码中设置的那样保持禁用状态.
Mar*_*Him 10
不要这样做 UIButton
nextOrCreateButton.titleView?.text = "FOOBAR"
Run Code Online (Sandbox Code Playgroud)
一直用
nextOrCreateButton.setTitle("FOOBAR", forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
或者swift3
nextOrCreateButton.setTitle("FOOBAR", for: .normal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
393 次 |
| 最近记录: |