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.
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"
Jhoan Arango14,575 Points
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.
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 :)