Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting a UILabel to an IBOutlet causes a crash

Tags:

iphone

I have a UIViewController I am loading from a xib file and pushing onto the navigation controller stack.

In the header file for the View Controller, I have:

IBOutlet UILabel *myTitle;

I don't do anything with "myTitle" in the code yet; I was just setting up the connections.

When I compile and run the application, and there are no labels defined in the xib file (and thus, nothing attached to the IBOutlet), it works. The view controller animates into view just fine, showing the view I built in Interface builder.

If I add a label to the xib in the interface builder, but don't connect it to the outlet, and recompile, it still works, showing the label with the default text I entered for it.

But if I connect the IBOutlet myTitle to the label in interface builder, recompile, and run the app, it works fine until I try to push the view controller onto the navigation controller's stack, at which point I get a crash:

*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x4558e20

If I disconnect the outlet again, it resumes working, showing the static label as before. It appears that there's something funky going on when the view gets displayed, because the crash happens when I push the view onto the navigation stack.

Am I not supposed to add an IBOutlet to a UILabel or something? Or is there something else going on? Any suggestions on where to look for trouble?

like image 803
CC. Avatar asked Oct 29 '09 17:10

CC.


4 Answers

Yes, it could be caused by wrong param name, e.g. try this:

IBOutlet UILabel title;

and it'll call an exception like that:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x143f520'
like image 119
slatvick Avatar answered Nov 15 '22 09:11

slatvick


Did you create an accessor for your IBOutlet?

@property (assign) IBOutlet UILabel *myTitle;

And then, in your .m file

@synthesize myTitle
like image 24
dj2 Avatar answered Nov 15 '22 10:11

dj2


I had the same problem. Turns out *title is reserved and Xcode doesn't tell you this. Once I renamed it to something else it worked.

like image 5
Flea Avatar answered Nov 15 '22 09:11

Flea


In my case I had a UILabel in a UIToolBar. What fixed it for me was to delete the toolBar and add a new one with a new UILabel. An IB bug.

like image 1
Joshua Goossen Avatar answered Nov 15 '22 08:11

Joshua Goossen