NSNotification
別のクラスの変数の値に影響を与える別のクラスから情報を渡すことができるようにしたいので、利用するプログラムを構築しています。
そのため、次のように設定しました。
categories.m
クラス:
でviewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTheScore:)name:@"TheScore" object:nil];
私の関数を使用して、同じクラスでupdateTheScore
:
- (void)updateTheScore:(NSNotification *)notification
{
NSLog(@"Notification Received. The value of the score is currently %d", self.mainScreen.currentScore);
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
でmainScreen.m
:
self.currentScore++;
[[NSNotificationCenter defaultCenter]postNotificationName:@"TheScore" object:self];
通常の場合、スコアは 0 から 1 に更新されます。
notification
実行されていることがわかるので、プログラムは を正しく呼び出しますNSLog
。ただし、変数の値が通過していません。これが私が立ち往生している場所です。
私の変数値が通過しない理由について、誰かが解決策を考えてもらえますか?
明確にするために、行の直前に NSLog を実行して、このpostNotificationName
値を表示するとself.currentScore;
、予想どおり 1 が返されます。updateTheScore
関数では、returns 0
.
みなさん、よろしくお願いします。