0

我正在开发一个带有推送通知的 iphone 应用程序。我的问题是,当我收到推送通知而不创建 classB 的实例或使用静态方法时,如何从 classB 调用函数?

非常感谢 :)

4

2 回答 2

1

您需要在接收推送通知的对象中保存对 classB 的引用:

// The header file of the class that receives the notification

@class classB;

@interface MyNotifiedClass : NSObject
{
    classB *_classB;
}

@property (retain, nonatomic, readwrite) classB *classB;

// The implementation file of the class that receives the notification

@implementation MyNotifiedClass

....

@synthesize classB = _classB;


- (void)theMethodThatReceivesTheNotification:(whatever)
{
    [_classB doSomethingNowPlease];
}

你显然必须classB在这个类中设置实例才能工作:

// Someplace outside both classes (assuming _classB points to an instance of classB
// and _myNotifiedClass points to an instance of MyNotifiedClass)

[_myNotifiedClass setClassB:_classB];
于 2012-05-04T09:36:16.500 回答
0

如果不实例化类,就无法调用类的实例方法。您的解决方案是调用类方法或classB可能具有单例初始化程序。

于 2012-05-04T09:27:16.247 回答