-1

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.

Silican
  • 124
  • 1
  • 12

1 Answers1

0

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.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Ok, I get that I shouldnt be using the same view for multiple controllers. But what if my View is like a Console, that needs to display error messages from Different parts of the application ? My thought here is to build this TextFieldMagic Object that will handle all the logs/errors that needs to be sent to the console [NSTextField]. I appreciate your quick response. – Silican Sep 07 '14 at 09:42
  • @Silican You expose a method in the controller that updates the view. – trojanfoe Sep 07 '14 at 10:19
  • 1
    Thanks for the explanation and your time. I took your suggestion and created a controller which takes care of updating the View. – Silican Sep 07 '14 at 11:53