gpt4 book ai didi

objective-c - NSNotificationCenter未发送通知

转载 作者:行者123 更新时间:2023-12-01 18:27:47 26 4
gpt4 key购买 nike

我正在开发一个跟踪成就的系统,为此,我使用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");
}

当我尝试发布通知时,未调用NSLogs。我使用以下代码从 AppDelegateViewController发送通知。
- (void)achievement1ButtonPressed:(id)sender {

[[NSNotificationCenter defaultCenter] postNotificationName:@"achievement1" object:self userInfo:nil];
}

我希望你们能帮助我。非常感谢
  • 乔纳斯
  • 最佳答案

    您甚至有一个名为receiveNotificationWithName的方法吗?当您输入代码时,自动完成功能应该被驳倒了,改为提供receiveNotificationWithName:(除非您首先编写它,没有NSNotification*,然后再添加它...

    无论如何,两者之间有很大的不同。另外,您的处理程序有故障。您应该重新访问该代码。

    有很多原因喜欢使用块,这就是其中之一。您可以将代码正确地放在注册中,而不必担心给出错误的选择器。

    [[NSNotificationCenter defaultCenter] addObserverForName:@"achievement1"
    object:nil
    queue:nil
    usingBlock:^{
    // Handle achievement1 right in this little block.
    }];

    这些看起来像是一次通知,因此,如果您想在运行一次后注销通知处理程序,请执行此操作(请注意 __block)。
    __block id achievement1 = [[NSNotificationCenter defaultCenter]
    addObserverForName:@"achievement1 ":^{
    object:nil
    queue:nil
    usingBlock:^{
    // Handle achievement1 right in this little block.

    // Remove observer from notification center
    [[NSNotificationCenter defaultCenter] removeObserver:achievement1];
    }];

    关于objective-c - NSNotificationCenter未发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11833718/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com