我想在我的项目中有六个按钮,并希望它们始终隐藏,除了一个。当我按下没有隐藏的按钮时,它应该被隐藏,并且另一个按钮应该随机出现并且执行相同的操作。 希望有人能帮助我!通过按下按钮随机显示按钮
回答
创建你的故事板6 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是我创建的做到这一点,你可以尝试一个样本项目。确保阅读上面代码中的注释并理解发生了什么。
谢谢你为我工作的人!但是,当我在我的项目中添加更多按钮时,按下六个按钮中的一个,它们也会消失。我该如何解决这个问题? –
我将此代码添加到我的项目中以隐藏除一个之外的每个按钮。这可以吗?重写FUNC viewDidAppear(_动画:BOOL){ BT6.isHidden =真 BT5.isHidden =真 BT4.isHidden =真 BT3.isHidden =真 BT2.isHidden =真 //隐藏除了一个当按钮视图控制器加载 } –
您需要做2件事:1:将它们从故事板拖到'@IBAction func button_clicked(_ sender:AnyObject)''2:在故事板中添加标签 –
我假设你已经把你的六个按钮放到你的故事板上并将它们链接到你的课堂上。 我做得很快,所以它可能不是最有效的方法。
你会希望你的类的代码看起来是这样的:
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
}
}
谢谢你,帮了我很多! –
UI(故事板)
在我的情况,六个按钮按钮标签数量各自为0分配给5.
//
// ViewController.swift
// StackOverflow
//
// Created by Seoksoon Jang on 2016. 10. 1..
// Copyright © 2016년 Seoksoon Jang. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var buttonTagNumberArray : Array<Int>?
var randomIndex : Int?
@IBOutlet var button1: UIButton!
@IBOutlet var button2: UIButton!
@IBOutlet var button3: UIButton!
@IBOutlet var button4: UIButton!
@IBOutlet var button5: UIButton!
@IBOutlet var button6: UIButton!
@IBAction func button1Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button1.tag) {
button1Action(button1)
} else {
button1.isHidden = true
switch randomIndex! {
case button1.tag :
print("it should happen : \(button1.tag)")
break
case button2.tag :
button2.isHidden = false;
break
case button3.tag :
button3.isHidden = false;
break
case button4.tag :
button4.isHidden = false;
break
case button5.tag :
button5.isHidden = false;
break
case button6.tag :
button6.isHidden = false;
break
default:
//
break;
}
return ;
}
}
@IBAction func button2Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button2.tag) {
button2Action(button2)
} else {
button2.isHidden = true;
switch randomIndex! {
case button1.tag :
button1.isHidden = false;
break
case button2.tag :
print("it should happen : \(button2.tag)")
break
case button3.tag :
button3.isHidden = false;
break
case button4.tag :
button4.isHidden = false;
break
case button5.tag :
button5.isHidden = false;
break
case button6.tag :
button6.isHidden = false;
break
default:
//
break;
}
return ;
}
}
@IBAction func button3Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button3.tag) {
button3Action(button3)
} else {
button3.isHidden = true;
switch randomIndex! {
case button1.tag :
button1.isHidden = false;
break
case button2.tag :
button2.isHidden = false;
break
case button3.tag :
print("it should happen : \(button2.tag)")
break
case button4.tag :
button4.isHidden = false;
break
case button5.tag :
button5.isHidden = false;
break
case button6.tag :
button6.isHidden = false;
break
default:
//
break;
}
return ;
}
}
@IBAction func button4Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button4.tag) {
button4Action(button4)
} else {
button4.isHidden = true;
switch randomIndex! {
case button1.tag :
button1.isHidden = false;
break
case button2.tag :
button2.isHidden = false;
break
case button3.tag :
button3.isHidden = false;
break
case button4.tag :
print("it should happen : \(button2.tag)")
break
case button5.tag :
button5.isHidden = false;
break
case button6.tag :
button6.isHidden = false;
break
default:
//
break;
}
return ;
}
}
@IBAction func button5Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button5.tag) {
button5Action(button5)
} else {
button5.isHidden = true;
switch randomIndex! {
case button1.tag :
button1.isHidden = false;
break
case button2.tag :
break
case button3.tag :
button3.isHidden = false;
break
case button4.tag :
button4.isHidden = false;
break
case button5.tag :
print("it should happen : \(button2.tag)")
break
case button6.tag :
button6.isHidden = false;
break
default:
//
break;
}
return ;
}
}
@IBAction func button6Action(_ sender: AnyObject) {
randomIndex = Int(arc4random_uniform(UInt32(buttonTagNumberArray!.count)))
if (randomIndex! == button6.tag) {
button6Action(button6)
} else {
button6.isHidden = true;
switch randomIndex! {
case button1.tag :
button1.isHidden = false;
break
case button2.tag :
button2.isHidden = false;
break
case button3.tag :
button3.isHidden = false;
break
case button4.tag :
button4.isHidden = false;
break
case button5.tag :
button5.isHidden = false;
break
case button6.tag :
print("it should happen : \(button2.tag)")
break
default:
//
break;
}
return ;
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
buttonTagNumberArray = [button1.tag, button2.tag, button3.tag, button4.tag, button5.tag, button6.tag]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} // class end
我只是想使用递归。 :p – boraseoksoon
欢迎堆栈溢出!请查看[如何提问](http://stackoverflow.com/help/how-to-ask)以改善您的问题。发布您尝试过的代码以及收到的错误。尽可能具体,因为它会导致更好的答案。 – David