1

I'm a novice at Swift programming.

I've created an app with multiple identical buttons. I created one button with an action and then copied this button multiple times so that it uses the same action.

When one of the buttons are pressed, the image for the button changes ('image2' from 'image1'), and the button is disabled.

All good and fine so far.

I've then tried to create a reset button, where the buttons that have been pressed are enabled again, and the image for them changed back to 'image1'.

I did this by creating a collection outlet for a single button and then adding some of the other ones, one-by-one.

@IBOutlet var mybuttons: [UIButton]!

As I understand this creates an array with the buttons. I then go over this array and update the image and enable the button, with a loop:

for myBubble in self.bubble

Now, my question is: Instead of having to drag each and every button to the outlet collection, would it be possible to add them to an array when they are pressed, and then run over that array instead?

I tried to create an array with

var mybuttons: [UIButton]!

and then use append(sender) when a button was pressed, but this gave a fatal error.

As mentioned I could solve this by simply adding all to an outlet collection but was wondering if there was a more elegant way to do it, since there are quite a lot of buttons.

As mentioned I'm a novice at this.

Thanks in advance.

Best regards, Thomas

1 Answer 1

3

You have just declared your button array but not initialized it:

var myButtons = [UIButton]()

This way you declare and initialize an empty array and you can start to append your objects to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.