Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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..