gpt4 book ai didi

ios - postNotificationName 尚未在 appdelegate 类中触发

转载 作者:行者123 更新时间:2023-12-01 20:13:17 26 4
gpt4 key购买 nike

在 Appdelegate.m 中添加了 postNotification 方法

   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

if(application.applicationState == UIApplicationStateBackground) {

[[NSNotificationCenter defaultCenter] postNotificationName: @"SuggetionPushNotification" object:nil userInfo:userInfo];


AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];

SideMenuViewController *leftMenuViewController = [[SideMenuViewController alloc] init];
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:[[UINavigationController alloc]
initWithRootViewController:[[SuggetionViewController alloc] init]]
leftMenuViewController:leftMenuViewController
rightMenuViewController:Nil];

[self.window makeKeyAndVisible];
appDel.window.rootViewController = container;

}


}

ViewController B (SuggetionViewController) 在 vi​​ewDidLoad
   [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"SuggetionPushNotification"
object:nil];



- (void) receiveTestNotification:(NSNotification *) notification {
NSLog(@"working");

}

但是这里还没有触发通知,如果在同一个类中添加了 post 和 addobserver,那么只有它会触发。我做错了什么。我从 Send and receive messages through NSNotificationCenter in Objective-C? 转介请帮忙

最佳答案

当您发布通知时,您的 View Controller B 不在内存中,这就是 Controller B 无法观察通知的原因。在发布通知之前添加延迟会有所帮助。

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[[NSNotificationCenter defaultCenter] postNotificationName: @"SuggetionPushNotification" object:nil userInfo:nil];

});

关于ios - postNotificationName 尚未在 appdelegate 类中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295687/

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