iOS app applicationWillEnterForeground并且它停留了一段时间

jxd*_*ter 9 background-foreground uiapplicationdelegate ios

我添加此函数以在应用程序进入前台时发布通知:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName: @"UIApplicationWillEnterForegroundNotification" object: nil];
}
Run Code Online (Sandbox Code Playgroud)

在我自己的班上:

- (void) handleEnterForeground: (NSNotification*) sender
{
    [self reloadTableData];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnterForeground:)
                                             name: @"UIApplicationWillEnterForegroundNotification"
                                           object: nil];
}
Run Code Online (Sandbox Code Playgroud)

但是handleEnterForeground:函数会调用两次,我不知道为什么.reloadTableData:函数将调用远程webService,因此当应用程序进入前台时,它会停留一段时间.

bor*_*den 17

系统将自动调用该事件.它发射两次的原因是你再次手动触发它.

PS最好使用变量名称UIApplicationWillEnterForeground,而不是NSString文字.

编辑:我现在意识到混乱来自于你不知道这个甚至名字已被采用的事实.作为遇到此类问题的其他人的注释,最好在事件名称前加上项目前缀(即XYZEventNotification)以避免冲突.

  • 而不是将其更改为另一个单词...为什么不只是取出该代码,并使用自动发生的事件? (2认同)