问题
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