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

时间:2012-05-18 07:24:20

标签: ios uiapplicationdelegate background-foreground

我添加此功能以在应用输入前景时发布通知:

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

在我自己的班上:

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


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

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

1 个答案:

答案 0 :(得分:17)

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

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

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