【问题标题】:iOS app applicationWillEnterForeground and it stuck for a whileiOS应用程序应用程序WillEnterForeground,它卡了一段时间
【发布时间】:2012-05-25 18:39:47
【问题描述】:

我添加了这个功能,当应用进入前台时发布通知:

- (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 ,所以当应用进入 前台,它会卡住一段时间。

【问题讨论】:

    标签: ios uiapplicationdelegate background-foreground


    【解决方案1】:

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

    附:最好使用变量名 UIApplicationWillEnterForeground,而不是 NSString 字面量。

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

    【讨论】:

    • 我确定我调用了该函数一次。
    • @cnu he 触发一次,iOS 运行时触发一次。这个事件会被自动调用。
    • 谢谢,现在好了!!!我将“UIApplicationWillEnterForegroundNotification”改为另一个词。
    • 与其把它换成另一个词...为什么不直接取出那个代码,并使用自动发生的事件呢?
    • 他的名字是borrrden,不是OLO。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    相关资源
    最近更新 更多