NSTextField unrecognized selectors

一曲冷凌霜 提交于 2020-01-07 06:47:09

问题


I have a text label declared as:

@property (weak) IBOutlet NSTextField *label;

I set some attributes in awakeFromNib method:

- (void)awakeFromNib {
    [label setStringValue:@"hello"];
}

And it all works. But when I try to change the string value (with setStringValue as well) somewhere else in the code I receive this error:

-[__NSCFString setStringValue:]: unrecognized selector sent to instance 0x105703040

I noticed it behaves the same way also with methods like isHidden, setHidden Any idea why?


回答1:


You need to learn how to read the error messages. You're being told that "setStringValue:" was "sent" to an NSString/CFString object. This means that the pointer in "label" is not an NSTextField but is instead an NSString. Most likely, at some prior point in your program, you assigned an NSString to "label" when you meant to do setStringValue or some such. Or else, since "label" isn't retained, the storage was reclaimed and then used for an NSString.




回答2:


The error message is telling you that you're sending the setStringValue: message to an NSString object, not an NSTextField object. Your awakeFromNib code is fine, but your code to change the label's string is wrong.



来源:https://stackoverflow.com/questions/7938750/nstextfield-unrecognized-selectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!