[UILabel copyWithZone:]: unrecognized selector sen

2019-01-17 13:57发布

问题:

I feel like I'm banging my head against the wall here (and I'm ready to do that, frankly).

I'm trying to set a background to the view. Background I need to set is an image and I'm using this code (which works perfectly fine in two other UIViewController subclasses - this was a copy/paste into the new file):

UIImage *img = [gameNode imageFromKey:@"background"];
UIColor *bg = [[UIColor alloc] initWithPatternImage:img];
self.view.backgroundColor = bg;
[bg release];

As I said, this works perfectly fine in two other places, however here, line self.view.backgroundColor = bg throws

'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x9259840'

There's not much more to go by. I verified in IB that the top-level view's referencing outlet is set to the File's owner. I cleaned the project and restarted. I even closed Xcode and restarted it again - no luck. What else am I missing?

Edit: As I mentioned, there's not much back stack trace, just this:

2012-05-28 13:13:56.997 OOKL[36012:17303] -[UILabel copyWithZone:]: unrecognized selector sent to instance 0x92b79a0
2012-05-28 13:13:56.997 OOKL[36012:17303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x92b79a0'
*** First throw call stack:
(0x2933052 0x44c7d0a 0x2934ced 0x2899f00 0x2899ce2 0x2935bd9 0xaa92dd 0x6a4f40 0x6a4eeb 0x6bfd60 0xc0191a 0x2934e1a 0x289e821 0xc0046e 0xaa7e2c 0xaa83a9 0xaa85cb 0xb2b59 0xb1969 0x2934ec9 0x9e55c2 0x9e555a 0xa8ab76 0xa8b03f 0xa8a2fe 0xa0aa30 0xa0ac56 0x9f1384 0x9e4aa9 0x2ac6fa9 0x29071c5 0x286c022 0x286a90a 0x2869db4 0x2869ccb 0x2ac5879 0x2ac593e 0x9e2a9b 0x214d 0x20c5)

Nothing else - before or after.

回答1:

I guess this was one of those weird bugs with Xcode that you can't trace or reproduce every time. I narrowed the issue down to one specific UILabel in the IB. As soon as I connect it to an IBOutlet UILabel *, I get this error.

On further investigation, it turns out that name title was causing the issue. I had the label declared as

IBOutlet UILabel *title;

As soon as I changed the name to gtitle, everything worked as expected. Who knows?..



回答2:

Try this :

self.view.backgroundColor = [UIColor clearColor];
self.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:img].CGColor;


回答3:

This definitely looks like an "over released" issue.

The reason is hard to tell in your example (since the code looks absolutely fine) but the fact that you're having a copyWithZone selector sent on an instance of UILabel (instead of UIColor) would definitely tell me that the bg has been over released and is now referring to a UILabel.

Check that later on in the code you do not again release the object bg.

Hope this helps

EDIT AFTER YOUR FIRST COMMENT

This is quite weird that you're receiving a copyWithZone unrecognized selector sent to instance, because I would believe that if your self.view was of the wrong type (not a UIView) then you would receive a setBackgroundColor unrecognized selector sent to instance and not a copyWithZone.

To better analyze this problem, when the exception is raised, I would write into the debugger:

po [self view]
po [[self view] backgroundColor]

to see if everything is working out smoothly as it should be.



回答4:

this issue happened to me last night and I actually did have a label named title. I tore a bit of my hair out when changing the name to something else didn't work. It turns out that I was attempting to send a NIL value from a text field to a label in a VC. I have 4 VC's and a user enters a title at VC 1 in the text field and that gets stored into an object of type NSString. That string is put into a label in VC 4. When a user doesnt enter a title in VC 1, that's where the NIL came in . I fixed it by assigning a default value to the string object when the user doesn't enter a title in VC1. Hope that helps someone.



回答5:

I had a similar issue that I managed to solve using the answers provided here.

In my case, however, I had a class called UIDateLabel (used to present dates in a formatted way etc). Compiling and running resulted in the NSInvalidArgumentException mentioned above.

I WAS suspecting that maybe there is a class called like that, but googling on UIDateLabel only showed some obscure hits (and none that linked to Apple as I could see). So I assumed that there was no naming problem for a while... until I read the answers in here and gave it a try.

So I replaced the name to UIDateLabelAbc and everything works perfectly. Renaming back to UIDateLabel does not work as XCode tells me that there is a class with that name already. So I did some more googling and found some wiki page that claims to list the UIKit.framework inheritance hierarchy, and in that list I found UIDateLabel listed. However, that's all the info I managed to find about that class, i.e. that it exists. What it does, its purpose and so on remains unknown to me at the moment.

Update: After reading about naming conventions I now realize that prefixing your classes with UI is not good practice (to say the least). So, don't prefix your classes with UI and you'll avoid this and probably a bunch of other problems.



回答6:

I get this error when I declare an IBOutlet variable, and give it the same name as something else in that view controller. Renaming my variables and functions more sensibly therefore solved the issue