【问题标题】:How to call a function when receive push notification?收到推送通知时如何调用函数?
【发布时间】:2012-05-04 08:26:22
【问题描述】:

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

非常感谢:)

【问题讨论】:

  • 我不知道你是否清楚你的对象生命周期。如果 ClassB 是一个 sigleton 类,那么只需将其用作访问器。如果您只想在 ClassB 的任何对象存在的情况下调用 ClassB 中的方法,则使用 NSNotification 机制,在创建 ClassB 时设置它并在 dealloc 时销毁它。简而言之:ClassB 的生命周期是回答您问题的重要部分;)

标签: objective-c ios methods apple-push-notifications


【解决方案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];

【讨论】:

  • 您好,感谢您的帮助,但我不明白您显然必须在此类中设置 classB 的实例,然后它才能工作。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多