Generating UIButtons from a loop, part 2


creating a second part of the tutorial as some folks wanted to do this trick with a few variations.
( link to part I )

As a start a Navigation controller got involved πŸ™‚
So you end up with a problem where buttons would disappear as you go back and forth.
Solution for this is posted at the end of part 1.
However there was someone who wanted to have different main button and the later created buttons.

Here is a solution to that.
First of all embed your project in a Navigation Controller, so you end up with something like that :


Then, change your project(ViewController.swift) to this:

Then, once you run the project, you should see this:

Once you add some buttons:

Now, if you go back and forth the buttons should still be there.

I was actually gonna add another example on how to just add the buttons to a new UIView and
just add it later, however I found some problems with doing that and a navigation controller, so that is probably the best way of doing it ( at least for now πŸ˜‰ )

Cheers !

8 thoughts on “Generating UIButtons from a loop, part 2

  1. I have another question, how do we layout the buttons in rows of 2,in terms of after 2 buttons it will automatically create the 3rd button on the second row, within the GUI size?Since my buttons are of circle type. You have been so much of a great help!!! Cheers mate πŸ˜€

  2. Razvigor Andreev says:

    Oh, well if you want to have 2 per row you would have to add a variable for the X offset as well.
    Then create flags( Bool ) that will check to see where we are at.
    Something like:

    var secondColumn: Bool = false

    var buttonY: Int = 20
    var buttonX: Int = 50

    then you will do something like this in your button create logic:

    let villainButton = UIButton(frame: CGRect(x: buttonX, y: buttonY, width: 250, height: 30))

    if secondColumn == true {

    buttonY = buttonY + 50 // we are going to space these UIButtons 50px apart, if next button is on the next row only
    buttonX = buttonX – 50 // go back to column 1 if this is column 2
    secondColumn = false

    } else {

    buttonX = buttonX + 50
    secondColumn = true


    Something like that, but I just typed it, haven’t actually ran it πŸ˜‰

  3. Hi! I hope I am not troubling you but there’s another problem that I faced 😑 I currently have dynamic butttons created every time the add new button is pressed, and all the dynamic buttons leads to the same view. The problem is I couldn’t display the individual button’s data when the button is selected. For e.g button 1 ‘s data when button 1 is selected and button 3 ‘s data when button 3 is selected and so on. Instead, it only shows the latest data inputted, which is then displayed on all the buttons… Thanks in advance! πŸ˜€ ( Data are retrieved from external SQLite database)

    • Razvigor Andreev says:

      Yeah, the problem is that in this method you were only using the String as the button name. Like I said in theory there is a better way, so you can either make the whole View a global variable ( that ends up being buggy, due to the UIButtons linking ) or I guess you can saving each button in an array instead. So instead of the String Array you will use an Array of [UIButton]. I have the feeling that this might not work as well πŸ™‚
      The other solution is to use another Array with the Buttons info in parallel ( or use a dictionary )
      So you can create a second Array for the Labels for example: var arrayOfNewButtonsLabels = [String]()
      And then when you create a new button you can append to it as well with the label.
      Then when you create the new buttons you will get the button Title and Label from the stored variables.
      It really depends on what exactly you want to do with it.