当一个按钮被按下时,如何在XCode中禁用所有按钮?

4

我该如何在XCode中按下一个按钮后,使其它按钮(包括被按下的按钮)都变为不可用状态?当然,我仍希望被按下的按钮能够执行相应的功能,但我不希望用户能够重复点击任何一个按钮,也不希望他们在已经按下一个按钮后再按下另一个按钮。以下是我两个按钮的IBActions:

@IBAction func addVote1(sender: AnyObject) {

    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("BiEM17uUYT") {
        (voteCount1: PFObject!, error: NSError!) ->Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
        }

        let votes = voteCount1["votes"] as Int
        let votes2 = voteCount1["votes2"] as Int
        self.pollResults1.text = "\(votes) votes                 \(votes2) votes"
        }
    }

@IBAction func addVote2(sender: AnyObject) {

    var query = PFQuery(className: "VoteCount")
    query.getObjectInBackgroundWithId("BiEM17uUYT") {
        (voteCount1: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount1.incrementKey("votes2")
            voteCount1.saveInBackgroundWithTarget(nil, selector: nil)
        }

        let votes = voteCount1["votes"] as Int
        let votes2 = voteCount1["votes2"] as Int
        self.pollResults2.text = "\(votes) votes                 \(votes2) votes"
        }
    }
}
5个回答

8

如果您还没有为按钮设置@IBOutlet属性,请先设置,然后添加一个懒加载的按钮数组lazy var。在按钮处理程序中,将每个按钮的enabled属性设置为false。

class ViewController {
    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!

    lazy var buttons: [UIButton] = [self.button1, self.button2]

    // ...

    @IBAction func addVote1(sender: AnyObject) {
        for button in self.buttons {
            button.enabled = false
        }
        // ...
    }
}

我已经为这两个按钮设置了IBOutlet属性。我需要在现有的IBOutlet属性中添加lazy var,还是需要完全创建新的IBOutlet属性? - ansariha
为参考添加了一些示例代码-你需要根据你的类进行调整。 - Nate Cook

2
你可以遍历UIView中的所有子视图,查找是否为UIButton,如果是,你可以禁用该按钮。
func disableButtons() {
        for views in view.subviews {
            if let button = views as? UIButton {
                button.enabled = false
            }
        }
    }

1
请花一分钟时间解释一下你的代码。当你解释代码时,很可能会得到一个加1的赞。 - DᴀʀᴛʜVᴀᴅᴇʀ
1
已编辑回答,谢谢。 - José Neto

1
最简单的方法是在顶部添加一个背景颜色为UIColor.clearColor()的UIView。它是不可见的并且可以捕获所有的点击。
class ViewController {
  private var uiBlocker = UIView()

  override func viewDidLoad() {
     uiBlocker.backgroundColor = UIColor.clearColor()
  }

  @IBAction func buttonAction() {
    view.addSubView(uiBlocker)

    [stuff you want to do]

    uiBlocker.removeFromSuperView()
  }
}

1

做一件事情,给所有按钮赋予唯一的标签。然后创建一个方法来创建按钮并使用按钮标签禁用它们。

func disableButton()
{
    for tagvalue in 101...102
    {
        var btnTemp = self.view.viewWithTag(tagvalue) as UIButton;
        btnTemp.enabled = false;
    }
}

请将以下代码添加到您的按钮中,如下所示。
@IBAction func addVote1(sender: AnyObject) 
{
  //Your code
  disableButton()
}

@IBAction func addVote2(sender: AnyObject) 
{
  //Your code
  disableButton()
}

我很不想问这个问题,但是你如何给一个按钮分配一个标签?我知道在Swift中,它是button.tag = 101,如果我想将标签101分配给我的其中一个按钮,但无论我把它放在哪里都不起作用。它在IBAction中不起作用,所以是在IBOutlet中吗? - ansariha
如果您是通过界面构建器进行设计,则可以通过属性检查器设置标记;如果您是以编程方式进行设计,则可以使用button.tag=101这样的语句。 - Indrajeet

0
let subviews : NSArray = headerView.subviews as NSArray
    for button in subviews {

      if let button = button as? UIButton {
            //let btn = button as! UIButton
            button.isSelected = false
        }

    }

    sender.isSelected = true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接