Communication of objective C between classes

综合技术 2018-01-01

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!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

Effective objective-C 读书笔记 (第一部分)... 第1章 熟悉Objective-C 第1条 了解Objective-C语言的起源 Objective-C是一种“消息结构”的语言,而非“函数调用”语言。 关键区别在于:使用消息结构的语言,其运行时所执行的代码由运行环境来决定;而使用函数调用语言,则由编译器决定。若是函数调用语言...
Is there a way to integrate Node.JS with Objective... I've done my node.js middleware in JSON for a realtime application on iPhone. I wanted to know if there's any way to prepare a client for iPhone...
#typedef and KVC in ObjC I have a class that looks like this: @interface Properties : NSObject { @private NSNumber* prop1; NSNumberBool* prop2; //etc where NSN...
iOS 开发 -《Effective Objective-C 2.0:编写高质量 iOS 与 OS ... 文章共分为三篇: 第一篇: iOS 开发 -《Effective Objective-C 2.0:编写高质量 iOS 与 OS X 代码的 52 个有效方法》读书笔记(1) 第二篇: iOS 开发 -《Effective Objective-C 2.0:编写高质量 iOS 与 OS X...
Optional Emptiness Objective-C developers are comfortable with many idioms that fall out of the safety of messaging nil. For example, consider a chunk of Objective-C cod...