Communication of objective C 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.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Communication of objective C between classes

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录