gpt4 book ai didi

ios - 通过按下按钮随机显示按钮

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:46 24 4
gpt4 key购买 nike

我想在我的项目中有六个按钮,并且希望除了一个按钮之外它们始终隐藏。当我按下未隐藏的按钮时,它应该被隐藏,另一个按钮应该随机出现并执行相同的操作。如果有人能帮助我,我将不胜感激!!

最佳答案

在 Storyboard 中创建六个 button,向它们添加一个标签,然后创建一个 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是我创建的一个示例项目,您可以尝试执行此操作。请务必阅读上面代码中的注释并了解发生了什么。

关于ios - 通过按下按钮随机显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com