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() {
    // Do any additional setup after loading the view.

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

    let buttonsView = UIView()
    buttonsView.translatesAutoresizingMaskIntoConstraints = false


        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
            print("the count of the buttons is \(buttons.count)")





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.


You are right. Thanks @NigelGee


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.