Search code examples
objective-cnstextfieldiboutlet

Getting NSTextField Value from another Class


I have a class called TextFieldMagic which handles the NSTextField manipulations, animation,validation etc.

I want to get the NSTextField instance from tne TextFieldMagic class in my AppDelegate. I tried the following and I couldnt get it working. I'm just getting (null)

- (IBAction)testHide:(id)sender {
    TextFieldMagic *textFieldMagic = [[TextFieldMagic alloc] init];
    NSLog(@"%@",[textFieldMagic.textField stringValue]); 
}

Of course, I could create an IBOutlet for NSTextField in my AppDelegate to get this working, but I want to understand how to do it w/o creating an IBOutlet in AppDelegate.


Solution

  • That's not the correct approach. With MVC, which is the design pattern adopted with Cocoa apps, the NSTextField is part of the View and should be used exclusively by the Controller in order to populate the Model.

    In other words you need to expose some or all of the Model data for other classes to use, and you should not be attempting to use the same View from multiple Controllers.