ios [UILabel copyWithZone:]: 无法识别的选择器发送到实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10784207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[UILabel copyWithZone:]: unrecognized selector sent to instance
提问by Aleks G
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):
我正在尝试为视图设置背景。我需要设置的背景是一个图像,我正在使用此代码(在其他两个 UIViewController 子类中工作得很好 - 这是复制/粘贴到新文件中):
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
正如我所说,这在其他两个地方非常有效,但是在这里,线self.view.backgroundColor = bg
抛出
'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?
没有更多的事情要做。我在 IB 中验证了顶级视图的引用出口设置为文件的所有者。我清理了项目并重新启动。我什至关闭了 Xcode 并重新启动了它 - 没有运气。我还缺少什么?
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.
没有别的 - 之前或之后。
回答by Aleks G
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.
我想这是 Xcode 的那些奇怪的错误之一,你不能每次都追踪或重现。我将问题缩小到 IB 中的一个特定 UILabel。一旦我将它连接到IBOutlet UILabel *
,我就会收到此错误。
On further investigation, it turns out that name title
was causing the issue. I had the label declared as
经过进一步调查,事实证明该名称title
是导致问题的原因。我将标签声明为
IBOutlet UILabel *title;
As soon as I changed the name to gtitle
, everything worked as expected. Who knows?..
一旦我将名称更改为gtitle
,一切都按预期进行。谁知道?..
回答by Maulik
Try this :
尝试这个 :
self.view.backgroundColor = [UIColor clearColor];
self.view.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:img].CGColor;
回答by apouche
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
.
在您的示例中很难说出原因(因为代码看起来非常好)但是您在(而不是)copyWithZone
的实例上发送了一个选择器这一事实 肯定会告诉我已经过度发布并且现在指的是到.UILabel
UIColor
bg
UILabel
Check that later on in the code you do not again release the object bg
.
稍后在代码中检查您是否不会再次释放该对象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
.
您收到 a 很奇怪copyWithZone unrecognized selector sent to instance
,因为我相信如果您self.view
的类型错误(不是 a UIView
),那么您将收到 asetBackgroundColor unrecognized selector sent to instance
而不是 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.
看看一切是否顺利进行。
回答by mafiOSo
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.
这个问题昨晚发生在我身上,我确实有一个名为 title 的标签。当将名称更改为其他名称不起作用时,我扯掉了一些头发。事实证明,我试图将 NIL 值从文本字段发送到 VC 中的标签。我有 4 个 VC,用户在文本字段中的 VC 1 处输入标题,然后将其存储到 NSString 类型的对象中。该字符串被放入 VC 4 中的标签中。当用户没有在 VC 1 中输入标题时,NIL 就出现在那里。当用户没有在 VC1 中输入标题时,我通过为字符串对象分配一个默认值来修复它。希望能帮助某人。
回答by Lukas Kalinski
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.
但是,就我而言,我有一个名为 UIDateLabel 的类(用于以格式化的方式显示日期等)。编译和运行导致上面提到的 NSInvalidArgumentException。
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.
我怀疑可能有一个这样的类,但在 UIDateLabel 上搜索只显示了一些晦涩的点击(我所看到的没有与 Apple 相关的)。所以我假设有一段时间没有命名问题......直到我阅读了这里的答案并尝试了一下。
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.
所以我将名称替换为 UIDateLabelAbc 并且一切正常。重命名回 UIDateLabel 不起作用,因为 XCode 告诉我已经有一个具有该名称的类。所以我做了更多的谷歌搜索,找到了一些声称列出 UIKit.framework 继承层次结构的 wiki 页面,在该列表中我发现列出了 UIDateLabel。然而,这就是我设法找到的关于该类的所有信息,即它存在。目前我对它的作用、目的等仍然一无所知。
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.
更新:在阅读了命名约定之后,我现在意识到用 UI 为类添加前缀并不是一个好习惯(至少可以这么说)。所以,不要用 UI 作为你的类的前缀,你会避免这个和可能的一堆其他问题。
回答by TimWhiting
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
当我声明一个 IBOutlet 变量并给它与该视图控制器中的其他东西相同的名称时,我会收到此错误。因此更明智地重命名我的变量和函数解决了这个问题
回答by Shakeel Ahmed
Swift 5 in my Case
Swift 5 在我的案例中
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)
{
}