TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Creating buttons programmatically using nested for loops

Forums > 100 Days of Swift

I posted this earlier today but upon reading it, I realized it made no sense, probably the result of working on this simple issue for hours nonstop! Anyways, please go through this code and tell me why the buttons's array which containts the letterButtons, has a count of 54 when it should have a count of 27. Thank you!

"""

import UIKit

class ViewController: UIViewController { var buttons = [UIButton]() var scoreLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    loadView()
    // Do any additional setup after loading the view.
}

override func loadView() {
    view = UIView()
    view.backgroundColor = .white
    scoreLabel = UILabel()
    scoreLabel.translatesAutoresizingMaskIntoConstraints = false
    scoreLabel.text = "words"
    view.addSubview(scoreLabel)

    let buttonsView = UIView()
    buttonsView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(buttonsView)

    NSLayoutConstraint.activate([

        scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 0),
        scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: 0),

        buttonsView.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 20),
        buttonsView.widthAnchor.constraint(equalToConstant: 750),
        buttonsView.heightAnchor.constraint(equalToConstant: 320),
        buttonsView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        buttonsView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -40)

    ])

    let width=76
    let height=55

    for row in 0..<3 {
        for col in 0..<9 {
            let letterButton = UIButton(type: .system)
            letterButton.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            letterButton.setTitle("W", for: .normal)
            let frame = CGRect(x: col * width, y: row * height, width: width, height: height)
            letterButton.frame = frame
            buttons.append(letterButton)
            buttonsView.addSubview(letterButton)
            print("the count of the buttons is \(buttons.count)")
        }
    }

}

}

"""

3      

Have var "newButtons = [UIButton]() inside the func loadView() with newButtons.append(letterButton) and then at after the loop buttons = newButtons.

I think the viewDidLoad() might be running more then once.

4      

You are right. Thanks @NigelGee

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.