加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:06:57 所属栏目:百科 来源:网络整理
导读:如何制作它以便当我在XCode中按一个按钮时,其余按钮(包括按下的按钮)将被禁用?当然我仍然希望通过按下按钮来执行该功能.我只是不希望用户能够多次按任何按钮,也不希望他们在按下第一个按钮之后再按下另一个按钮.以下是我在这种情况下的两个按钮的IBActions
如何制作它以便当我在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
        }
        // ...
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读