Objective C communication between classes
I have an AppController class that looks after view/control in my app in the usual way.
There's a button on my app's main window in IB that causes AppController to instantiate a new window controller (accountPanelController) and show that secondary window:
- (IBAction) showAccountPanel:(id) sender
{
//Is accountController nil?
if (!accountPanelController) {
accountPanelController = [[AccountPanelController alloc] init];
}
[accountPanelController showWindow:self];
}
When that new window is done with, I want to send the data collected from my secondary window controller to a method in AppController:
- (IBAction) close: (id) sender
{
NSLog(@"Close detected");
[AppController addAccount:0];
[self close];
}
However, when I try and call the addAccount method in AppController from the new window controller, I get an "'AppController' may not respond to '+addAccount'" warning.
This seems to be related to AppController being a class rather than an object instantiation, since the method in AppController is called -addAccount (rather than the +addAccount reported in the warning). Indeed, if I change the name of the target method to +addAccount instead of -addAccount, the warning does not appear (but the program crashes on execution).
Given that I don't actually开发者_如何学运维 instantiate AppController myself (I guess that happens somehow during NIB initiation), does anyone have any ideas how I can send the data to the AppController method? Notifications seem like overkill...
Many thanks.
I recommend the following introduction article on Apple's Mac Dev Center: Communicate with Objects - #Notifications
Update:
I pointed the link to to relevant anchor (Notifications).
The problem in your code sample is, that you call a class method (those with a +), but you implement an instance method (-).
So a simple fix would be, to get the (shared)instance of your AppController
(probably self
in your code) and send it the addAccount:
message.
But I encourage you to read the article first.
Maybe you can solve your problem by sending a notification (NSNotification) from your view to your controller.
Update:
Another interesting read for you might be this SO question regarding the difference between class methods and instance methods.
Just supply an intiWithOtherController method and add it herre:
accountPanelController = [[AccountPanelController alloc] initWithOtherController:self];
just pass self, so you need something like this:
(AccountPanelController *) initWithOtherController:(OtherController *)
now you have a pointer to otherController and you can do:
[otherController addAccount:0]
精彩评论