Juli IchSelber Juli IchSelber - 2 months ago 7
iOS Question

Show buttons randomly by pressing them

I want to have six buttons in my project and want them to be always hidden except one. And when I press the button that is not hidden it should be hidden and another button should randomly appear and do the same.
Would appreciate if someone could help me!!

Answer

I assume you have put your six buttons onto your storyboard and linked them into your class. I did this pretty quickly so it's probably not the most efficent way.

You would want your class' code to look something like this:

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