ios – 如何在XCode中按下所有按钮时禁用所有按钮?

前端之家收集整理的这篇文章主要介绍了ios – 如何在XCode中按下所有按钮时禁用所有按钮?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何制作它以便当我在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"
        }
    }
}

解决方法

如果尚未设置按钮的@IBOutlet属性,则添加按钮的延迟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
        }
        // ...
    }
}

猜你在找的Xcode相关文章