[UILabel copyWithZone:]: unrecognized selector sent to instance
Asked Answered
S

8

63

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.

Swinge answered 28/5, 2012 at 11:50 Comment(2)
Is your labels are properly connected in this view?Seems like an issue with label in the IB.Create an accessor like this in your .h file . @property (assign) IBOutlet UILabel *label;Metritis
In your gdb or lldb console, type bt to get backtracePontificals
S
197

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

Swinge answered 28/5, 2012 at 12:42 Comment(6)
Just my problem, using title. The thing is, in another file I use title too, but all just fine.Izard
This was killing me... thank you! In hindsight, definitely a poor choice for the ivar, but it wasn't critical so I named it by default. Super poor error message from XcodeMalkamalkah
@Orlando Leite. 'title' causes a problem because you were in a 'UIViewController' subclass. The 'UIViewController' class already has a member title. If you have a navigation bar , you can set the 'title' string and it will appear on the navigation bar. So if you are treating 'title' as a label you get errors because 'title' is in fact a 'NSString'.Greenway
I also had to rename title to somthing and also reconnect it run and again connectEntoderm
I always prefix the variable with an abbrev. control type. e.g. tvNotes for text view, lblName for label, ... helps avoid any manglingOrcus
@George: Thanks. Otherwise crashing on variable name wording is a HUGE bug that one should never just ignore until they figure out what's going on.Mirella
T
3

In my case it was a previously added IBOutlet which was connected to an item in the view in story board.

So you can carefully review the view in story board and look for all the connections and make sure if any of them is not messing up.

Taurus answered 19/4, 2022 at 10:41 Comment(0)
M
1

Swift 5 in my Case

YOURBUTTON.addTarget(self, action: #selector(funCross), for: .touchUpInside)

//MARK:- My Function 
@objc func funWithSearch(screen: String)
{

}

if you focus on #selector(funCross) i am calling a function without object

when i use this its work fine

YOURBUTTON.addTarget(self, action: #selector(funTemp), for: .touchUpInside)

//MARK:- My Function 
@objc func funTemp()
{
    funWithSearch(screen: "etc your desire string")
}

//MARK:- My Function 
@objc func funWithSearch(screen: String)
{

}
Mockingbird answered 9/8, 2019 at 6:55 Comment(0)
A
0

Try this :

self.view.backgroundColor = [UIColor clearColor];
self.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:img].CGColor;
Arabesque answered 28/5, 2012 at 11:58 Comment(1)
Tried this - self.view.backgroundColor = [UIColor clearColor] results in exactly the same error.Swinge
A
0

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.

Amp answered 28/5, 2012 at 12:0 Comment(2)
The problem seems to be with self.view, because self.view.backgroundColor = [UIColor whiteColor] results in exactly the same error.Swinge
quite weird, I've edited my answer so that it's clearer than a simple comment.Amp
S
0

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.

Sinistrodextral answered 23/9, 2012 at 20:42 Comment(0)
N
0

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.

Ninety answered 3/9, 2013 at 11:48 Comment(0)
O
0

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

Overmeasure answered 4/3, 2015 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.