按钮似乎仅在多次单击后禁用

时间:2015-08-06 14:00:52

标签: ios swift button

enter image description here

@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()

我已经尝试更改标签,看看这是不是为什么,但唉,这并没有做太多。这似乎正在发生,这很奇怪。当然,我只需点击一下就可以禁用它。

编辑 - 标签

enter image description here

1 个答案:

答案 0 :(得分:0)

如何以编程方式设置它? 另外,在将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
}