私は成果を追跡するためのシステムを開発しており、そのために使用していますNSNotificationCenter
。アプリ内のオブジェクトでアチーブメントのロックが解除されると、通知が送信されJMAchievementHandler
、文字列がに設定さYES
れ、進行状況がに保存されNSUserDefaults
ます。私の問題は、通知がおそらく機能していないことです。これが私のコードですJMAchievementHandler
:
- (id)init {
self = [super init];
if (self) {
//Set strings to NO
achievementOne = @"NO";
achievementTwo = @"NO";
achievementThree = @"NO";
achievementFour = @"NO";
//Add observers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationWithName:) name:@"achievement1" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationWithName) name:@"achievement2" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationWithName) name:@"achievement3" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationWithName) name:@"achievement4" object:nil];
//Add observer to observe delegate methods
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationFromDelegate:) name:@"notificationLaunch" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationFromDelegate:) name:@"notificationEnterBackground" object:nil];
}
return self;
}
- (void)receiveNotificationWithName:(NSNotification *)notification {
if ([[notification name] isEqualToString:@"achievement1"] || [achievementOne isEqualToString:@"NO"]) {
//unlock achievement
NSLog(@"%@ is unlocked", [notification name]);
achievementOne = @"YES";
}
else if ([[notification name] isEqualToString:@"achievement2"] || [achievementTwo isEqualToString:@"NO"]) {
//unlock achievement
NSLog(@"%@ is unlocked", [notification name]);
achievementTwo = @"YES";
}
else if ([[notification name] isEqualToString:@"achievement3"] || [achievementThree isEqualToString:@"NO"]) {
//unlock achievement
NSLog(@"%@ is unlocked", [notification name]);
achievementThree = @"YES";
}
else if ([[notification name] isEqualToString:@"achievement4"] || [achievementFour isEqualToString:@"NO"]) {
//unlock achievement
NSLog(@"%@ is unlocked", [notification name]);
achievementFour = @"YES";
}
}
- (void)receiveNotificationFromDelegate:(NSNotification *)notificationDelegate
{
if ([[notificationDelegate name] isEqualToString:@"notificationLaunch"]) {
[self loadDataOnLaunch];
}
else if ([[notificationDelegate name] isEqualToString:@"notificationEnterBackground"]) {
[self saveDataOnExit];
}
}
- (void)loadDataOnLaunch
{
NSLog(@"loadDataOnLaunch");
}
- (void)saveDataOnExit
{
NSLog(@"saveDataOnExit");
}
通知を投稿しようとすると、NSLogが呼び出されません。AppDelegate
次のコードを使用して、およびから通知を送信しますViewController
。
- (void)achievement1ButtonPressed:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"achievement1" object:self userInfo:nil];
}
皆さんが私を助けてくれることを願っています。どうもありがとう
- ジョナス