UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.