0

I am doing following things in my sample application:

I made a model class and declared an array as property in it.

@interface MyModel : NSObject

@property (strong, readwrite) NSArray *charactersToDisplay;

@end

In my app delegate class, I declared MyModel as a property.

@interface MyAppDelegate : NSObject <NSApplicationDelegate>

@property (strong, readwrite) MyModel *myModel;
@property (weak) IBOutlet NSObjectController *myModelController;

- (IBAction)test:(id)sender;

@end

In MainMenu.xib, I dragged and dropped an object controller object and performed its binding to myModel in MyAppDelegate.

I am trying to achieve following thing:

I have defined an action test:, which is bind to a button, in MyAppDelegateChange and trying to change value in charactersToDisplay.

Problem is:

When I am using this code, it is not working:

[self.myModel setCharactersToDisplay:[[NSArray alloc] initWithObjects:@"1",@"2", @"3", "@4", @"5", nil]];

but, if I am trying to change its value through object controller using below code, it is working:

[[self.myModelController content] setCharactersToDisplay:[[NSArray alloc] initWithObjects:@"1",@"2", @"3", "@4", @"5", nil]];

Can anyone suggest me - why it is not working when I am trying to change the value directly by calling setter method on myModel ?

Devarshi
  • 16,440
  • 13
  • 72
  • 125
  • How did you set up the model controller's content? – Ken Thomases Apr 20 '12 at 13:27
  • Through XIB using bindings... Content Object: App Delegate, ModelKeyPath: myModel – Devarshi Apr 22 '12 at 13:27
  • Try logging the values of `self.myModel` and `[self.myModelController content]`. Apparently they are different. Where do you set/change the `myModel` property? Could you be doing that in a non-KVO-compliant manner? It seems improbably from your question, but could you accidentally have two instance of `MyAppDelegate`, one instantiated in the NIB and another in code? – Ken Thomases Apr 22 '12 at 13:35
  • I am not setting myModel property anywhere, it is simply synthesized in .m and binded to object controller, and I have only one instance of MyAppDelegate instantiated in NIB – Devarshi Apr 22 '12 at 13:56
  • Then that's your problem. You need to set `myModel` to refer to an instance of `MyModel` somewhere. Good choices are in the init method, in `-applicationWillFinishLaunching:`, or maybe `-awakeFromNib`. – Ken Thomases Apr 22 '12 at 14:22

1 Answers1

0

That is because if you want to access myModel from the appDelegate you need to make it have IBOutlet, then connect that outlet in interface builder. Add IBOutlet to myModel as below.

@interface MyAppDelegate : NSObject <NSApplicationDelegate>

@property (strong, readwrite) IBOutlet MyModel *myModel;
@property (weak) IBOutlet NSObjectController *myModelController;

- (IBAction)test:(id)sender;

@end

Then connect that outlet to the MyModel object inside of interface builder.

Blake R
  • 101
  • 1