如何在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"
}
}
}
答案 0 :(得分:7)
如果您尚未设置按钮的@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
}
// ...
}
}
答案 1 :(得分: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()
}
答案 2 :(得分: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()
}
}
答案 3 :(得分:1)
你可以遍历UIView中的所有子视图并查找是否是UIButton,如果你可以禁用该按钮。
func disableButtons() {
for views in view.subviews {
if let button = views as? UIButton {
button.enabled = false
}
}
}
答案 4 :(得分: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