Cocoa textfield returning null
Sorry if it's a bit long but I want to be as descriptive as possible. I'm making a test application with several classes and 2 nib files (each class is the owner of one nib). One of the nib files has a textfield, the other has a button. I'm trying to log whatever is inside the text field when the button in the other nib view is pressed, but it returns (null). In one of the classes (ViewClass) I have this:
- (IBAction)startAction:(id)sender {
MyClass *anInstance = [[MyClass alloc] init];
NSString *string= [anInstance name];
NSLog(@"startAction logged: %@", string);
"Name" is a property of MyClass. What I want to do is have "name" set in the init of MyClass, that way, when anInstance is initialized, the MyClass init method does this:
- (id)init {
if ( self = [super init] ) {
[self setName:[nameInput stringValue]];
NSLog(@"init value: %@", name);
}
return self;
"NameInput" is the textfield. I thought this would return whatever was in the textfield, but I get null instead. When I use setName:@"text"
it gets passed fine, so something is wrong with the text field.
I previously did this with my own getter, and in that case, it didn't return null when the method was called from it's own class, but if it was initialized and called from the other class, then it returned null, I used this:
- (NSString *)name {
NSLog(@"nameMethod = %@", [nameInput stringValue]);
return [[[nameInput stringValue] retain] autorelease];
This way, I can tell it is all properly set up, but something is happening when I init MyClass from the ViewClass, and try to get "name", that it keeps saying that the text field is null.
Not sure if it helps but the nib with the button belongs to MyView (which is a subclass of NSViewController) and the textfield belongs to MyClass (subclass of NSObject).
Someone suggested the field was not properly linked, but if that was true, it wouldn't have worked when called from it's own class, but it did. Someone else mentioned it might be a problem with the textfield being initialized to nil, s开发者_如何学编程o I tried the init thing above. Neither has worked so far.
Thanks for the help.
IBOutlets are not guaranteed to be hooked up until awakeFromNib
, which is after your objects' init methods have run. You'll need to do your nib setup in awakeFromNib
.
EDIT NOW THAT I'VE GOTTEN HOME: Sorry, I didn't read carefully enough before. What I said above was true, but there's a deeper problem as well. I see now that you also are dealing with two different objects — one in the nib, one created in code. If you have an object in a nib with an outlet hooked up to an interface element, that doesn't make other objects of that class also have an instance variable referring to the element. Two independently created MyClass instances don't share the same instance variables any more than every NSArray in your program holds the same set of items. If you want to use the instance from the nib, you'll need to use that instance.
How you do this is a matter of how you structure your program. There's no simple [self magicallyGetObjectFromNib]
. Somehow, one object needs to either find the other (say, by knowing the nib's owner) or be told about the other by an object that knows about them both.
You're in the init routine when you try to extract a value from a field in the same object, and apparently expect it to be initialized. 'Tain't gonna happen.
精彩评论