Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Title of a button

Hi. I have created a button in Xcode and connected it to a VC using an IBOutlet. In viewdidload I am trying to set the button title using:

self.button1.setTitle?("TEST", for: .normal)

but I get "fatal error: unexpectedly found nil while unwrapping an Optional value"

Any ideas?

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Not that this will make a difference, but you do not have to call self. Self should be use only inside a closure or to distinguish a property name from the name argument in an initializer.

Now on your nil, make sure that you have the connections correct. Maybe at some point, you disconnected and reconnected the button. Right click on the button, and make sure that your outlet is connected, and delete any other outlets that do not belong there.

Example:

Image

Hi Jhoan. You were right. I had disconnected and reconnected the button without recreating the outlets. Fixed that and lo and behold, it works.

Thanks for the tip on "self" too :)

Jhoan Arango
Jhoan Arango
14,575 Points

Glad it worked:

You are welcome..