Doughnut Man Doughnut Man - 1 year ago 88
iOS Question

Swift: How a Button Can Perform a Sequence

How can a button unhid textfields each time its pressed and then after 4 times of being pressed the button becomes inactive? Here is my code so far. Thanks in advance!

import UIKit

class AddNewGameViewController: UIViewController {

@IBOutlet weak var Player1: UITextField!
@IBOutlet weak var Player2: UITextField!
@IBOutlet weak var Player3: UITextField!
@IBOutlet weak var Player4: UITextField!

override func viewDidLoad() {

// Do any additional setup after loading the view.

Player1.hidden = true
Player2.hidden = true
Player3.hidden = true
Player4.hidden = true


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

@IBAction func addPlayer(sender: UIButton) {
sender.enabled = false

Player1.hidden = false
Player2.hidden = false
Player3.hidden = false
Player4.hidden = false


Answer Source

change your @IBOutlets to a @IBOutlet Collectionlike this :

@IBOutlet var players: [UITextField]!

then connect each textfield to it Player1->Player2-> Player3->Player4

finally in the btn action :

for textField in players {

    if textField.hidden {
        textField.hidden = false



you can hide all them with players.forEach {$0.hidden = true} in viewDidload or hide them in xib/storyboard.

connect image :

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download