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

Show buttons randomly by pressing them

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

我假设您已将六个按钮放到故事板上并将它们链接到 class。 我做的很快,所以这可能不是最有效的方法。

您希望您的 class' 代码看起来像这样:

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
    }
}

在您的情节提要中创建六个 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 是我创建的一个示例项目,您可以尝试执行此操作。请确保阅读上面代码中的注释并了解发生了什么。

UI(故事板)

在我的例子中,六个按钮的按钮标签编号分别分配了 0 到 5。

//
//  ViewController.swift
//  Whosebug
//
//  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