2012-05-04 35 views
0

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

非常感谢:)

+0

我不知道你是否清楚你的Object Livecycles。如果ClassB是一个sigleton类,那么只需使用它作为一个访问器。如果你只是想调用ClassB中的方法,如果ClassB的任何对象存在,则使用NSNotification机制,在创建ClassB时将其设置并在dealloc上销毁它。为了减少这一点:ClassB的生命周期是解决你的问题的重要部分;) – monkeydom

回答

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]; 
+0

你好,谢谢你的帮助,但是我不明白你明明不得不在这个类中设置classB的实例。 – Veer

+0

@Veer好的,我已经用附加代码更新了我的答案。 – trojanfoe

0

有没有办法调用一个类的实例方法没有实例化它。您的解决方案是调用类方法或classB可能有单例初始化程序。

相关问题