2012-05-04 7 views
0

私はプッシュ通知付きのiPhoneアプリを開発中です。私の質問は、クラスBのインスタンスを作成せずにプッシュ通知を受け取ったときにクラスBから関数を呼び出す方法も、静的メソッドを使用することですか?プッシュ通知を受信したときに関数を呼び出す方法は?

多くのありがとう:)

+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 OK、追加コードで回答を更新しました。 – trojanfoe

0

クラスのインスタンスメソッドをインスタンス化せずに呼び出す方法はありません。あなたの解決策は、クラスメソッドを呼び出すか、classBがシングルトン初期化子を持つことができます。

関連する問題