【问题标题】:Button seems to only disable after multiple clicks按钮似乎只有在多次点击后才会禁用
【发布时间】:2015-10-29 17:20:54
【问题描述】:

@IBAction func topButton(sender: AnyObject) {
    let nicebutton = sender as! UIButton

    nicebutton.enabled = false

    let nopebutton = nicebutton.superview?.viewWithTag(102) as! UIButton
    nopebutton.enabled = true

    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
    object.incrementKey("count")
    self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.Automatic)
    object.saveInBackground()

}


@IBAction func bottomButton(sender: AnyObject) {

    let nopebutton = sender as! UIButton

    nopebutton.enabled = false

    let nicebutton = nopebutton.superview?.viewWithTag(101) as! UIButton

    nicebutton.enabled = true
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
    object.incrementKey("count", byAmount: -1)
    self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.Automatic)
    object.saveInBackground()

我已经尝试更改标签以查看是否是原因,但遗憾的是,这并没有起到太大作用。这似乎正在发生,这很奇怪。当然我宁愿一键禁用。

编辑 - 标签

【问题讨论】:

  • 如果 topButton 由带有标签 101 的按钮调用,bottomButton 由带有标签 102 的按钮调用,则此设置适用于我。您是否 100% 确定您的标签和 IBAction 已挂钩正确吗?
  • 您在使用 TouchUpInside 吗?也许另一种事件类型在您的场景中效果更好,您可以尝试 TouchDown... 似乎适用于 stackoverflow.com/questions/10101253/…
  • @SteveWilford 我相信是这样,请注意更新后的帖子。
  • @NullPointer 我想我可能必须尝试一下。

标签: ios swift button


【解决方案1】:

以编程方式设置它怎么样? 此外,请确保在将 IBAction 链接到按钮单击时使用 touchUpInside

// In your code, set these as the IBOutlets to your actual buttons
var nice = UIButton()
var nope = UIButton()

enum MyButton {
    case Nice
    case Nope
}

func setAvailability(button: MyButton) {
    nice.enabled = (button == .Nope)
    nope.enabled = (button == .Nice)
}

现在,您可以在方法调用中调用此函数:

@IBAction func topButton(sender: AnyObject) {
    setAvailability(.Nice)
    // ...other stuff
}

【讨论】:

  • 我试过这个方法,虽然它没有返回任何错误,但效果并不好。看起来它应该可以工作,但逻辑是合理的。
  • 在什么情况下它不起作用?也可以尝试将.enabled = ... 放在后台线程中,也许它更新 UI 的速度不够快?
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 2019-07-13
  • 2020-10-09
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
  • 1970-01-01
相关资源
最近更新 更多