我想在我的项目中有六个按钮,并希望它们始终隐藏,除了一个。当我按下未隐藏的按钮时,它应该被隐藏,另一个按钮应该随机出现并执行相同的操作。 如果有人可以帮助我,我将不胜感激!
答案 0 :(得分:4)
在故事板中创建六个buttons
,为其添加标签,然后创建一个Action outlet
,将所有按钮连接到,然后执行以下操作:
@IBAction func button_clicked(_ sender: AnyObject) {
// generate a random number which is not the same as the tag that you
repeat{
random = Int(arc4random_uniform(6) + 1)
}
while random == sender.tag
// iterate through all subviews in your view to find all buttons
for view in self.view.subviews{
// make sure it´s a button
if view.isKind(of: UIButton.self){
// create a button from the view you're iterating to
let button = view as! UIButton
// if the button tag is equal to the random number you just created we want to show that button
if button.tag == random{
button.isHidden = false
}
// else hide it
else{
button.isHidden = true
}
}
}
}
Here是我创建的示例项目,您可以尝试这样做。确保阅读上面代码中的注释并了解发生的情况。
答案 1 :(得分:2)
<强> UI(故事板)强>
在我的情况下,六个按钮的按钮标签编号分别从0到5分配。
<强> 强>
Row
答案 2 :(得分:0)
我假设您已将六个按钮放在故事板上并将它们链接到您的课程中。 我很快就这样做了,所以它可能不是最有效的方法。
您希望您的班级代码看起来像这样:
override func viewDidAppear(_ animated: Bool) {
BT6.isHidden = true
BT5.isHidden = true
BT4.isHidden = true
BT3.isHidden = true
BT2.isHidden = true
//Hiding all but one button when the view controller loads
}
@IBOutlet weak var BT6: UIButton!
@IBOutlet weak var BT5: UIButton!
@IBOutlet weak var BT4: UIButton!
@IBOutlet weak var BT3: UIButton!
@IBOutlet weak var BT2: UIButton!
@IBOutlet weak var BT1: UIButton!
@IBAction func BT6(_ sender: AnyObject) {
//this checks when BT6 is pressed and then hides it
BT6.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
//this part creates a randomiser between 0-4 and depending on which number turns out, it will hide a certain button
}
@IBAction func BT5(_ sender: AnyObject) {
BT5.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT6.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
}
@IBAction func BT4(_ sender: AnyObject) {
BT4.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT6.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
}
@IBAction func BT3(_ sender: AnyObject) {
BT3.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT6.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
}
@IBAction func BT2(_ sender: AnyObject) {
BT2.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT6.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
}
@IBAction func BT1(_ sender: AnyObject) {
BT1.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT6.isHidden = false
}
}