进入前台后刷新数据 [英] refresh data after entering a foreground

查看:114
本文介绍了进入前台后刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改默认设置后,我想在AppDelegate中输入前景时刷新myViewController的数据。我的工作是

After changing the default settings, I would like to refresh data of myViewController when I enter the foreground in AppDelegate. What I do is

AppDelegate.m

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:[navigationController view]];
    NSLog(@"APPLICATION DID FINISH LAUNCHING");
    // listen for changes to our preferences when the Settings app does so,
    // when we are resumed from the backround, this will give us a chance to update our UI
    //
    [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(defaultsChanged:)
                                           name:NSUserDefaultsDidChangeNotification
                                         object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
     */
    NSLog(@"APPLICATION WILL ENTER BACKGROUND");
    [myViewController viewDidLoad];
}

myViewController.m

myViewController.m

- (void)viewDidLoad
{
    NSLog(@"VIEW DID LOAD IN MY VIEW CONTROLLER");
    // watch when the app has finished launching so we can update our preference settings and apply them to the UI
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSettings:) 
                                        name:UIApplicationDidFinishLaunchingNotification object:nil];
}

- (void)updateSettings:(NSNotification *)notif
{
    myLabel.text = @"data has just been modified";
}

然而,没有任何改变。

推荐答案

您的代码中有2个问题。首先,启动函数的序列(所谓的执行状态)似乎不清楚,其次你为函数 updateSettings 添加了一个监听器,但你从未调用它在上面的代码中,这就是为什么你的应用程序启动时没有任何改变的原因。

You have 2 problems in your code. First the sequence of the start-up functions (what is called Execution States) seems not clear to you, and second you added a listener to the function updateSettings but you never called it in the code above, and this is why nothing changed when your app starts.

让我先解释一下启动顺序。当应用程序从关闭的设备加载时会触发这些状态:

Let me first explain the start-up sequence. When an app loads from a turned off device these states are fired:

application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:

之后如果您按下主页按钮,则会触发这些状态:

After that if you press the Home button these states are fired:

applicationWillResignActive:
applicationDidEnterBackground:

然后,如果再次进入应用程序,将发生以下情况:

Then if you enter into the app again, the following will occur:

applicationWillEnterForeground:
applicationDidBecomeActive:

请注意,加载状态仅在第一次加载时出现一次(但在从主页按下后返回时不会出现)。现在,对于每个视图,函数 viewDidLoad 将只调用一次,这是第一次调用此视图。如果再次调用此视图(在加载之后),则将调用函数 viewWillAppear 。所以通常刷新发生在 viewWillAppear 函数中。

Notice that the loading state only occurs once at the first load (but not after you return from a Home press). Now for every view the function viewDidLoad will be called only one time which is the first time this view was called. If call this view again (after it has been loaded) then the function viewWillAppear will be called instead. So usually refreshing occurs in viewWillAppear function.

我在你的代码中注意到的一个重要的是不正确的是主代表函数的用法。在 applicationWillEnterForeground 你手动调用 viewDidLoad ,而你不应该这样做,因为这个函数将自动调用,如上所述。此外,我发现您正在添加不需要的通知中心。

An important thing that I noticed in your code which is improper is the usage of the main delegate functions. In applicationWillEnterForeground you manually called viewDidLoad while you shouldn't do that as this function will be called automatically as I explained above. Also I see you are adding Notification centers that are not needed.

现在让我们看一下代码中的第二个问题。您正在 viewDidLoad 中为函数 updateSettings 添加通知中心。那么这个视图的加载将在事件 UIApplicationDidFinishLaunchingNotification 之后发生,因此实际上你从未调用函数 updateSettings 。此外,由于此功能是您班级的成员,因此您不需要通知中心来调用它。当我们需要从另一个类调用函数时,我们通常使用通知中心。您需要做的就是直接从 viewDidLoad 调用此函数,如下所示:

Now lets see the second problem in your code. You are adding a Notification Center for the function updateSettings in the viewDidLoad. Well the loading of this view will occur after the event UIApplicationDidFinishLaunchingNotification therefore in practice you never called the function updateSettings. Moreover since this function is a member of your class you don't need a Notification Center to call it. We usually use a Notification Center when we need to call a function from another class. Simply what you need to do is call this function directly from viewDidLoad as follows:

[self updateSettings]

如果您需要在按下主页按钮后更新,请致电函数来自 viewWillAppear

And if you need to update after the home button is pressed call the function from viewWillAppear.

我希望这个快速解释可以帮助你。

I hope this fast explanation helps you.

编辑:回答下面的评论

如果您只有一个视图(没有导航控制器...),之后它似乎第一次停留在内存中并且不会再出现(因此不会调用此函数)。在这里你应该抓住事件 UIApplicationDidBecomeActiveNotification 所以做以下事情:

If you have only one view (no navigation controllers...), after it appears the first time it will stay in the memory and it won't appear again (so this function isn't called). Here you should catch the event UIApplicationDidBecomeActiveNotification so do the following:

viewDidLoad 添加通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSettings) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];

这将允许函数 updateSettings 每次你的应用程序唤醒时调用。还记得在最后删除这个监听器:

This will allow the function updateSettings to be called every time your app wakes. Also remember to remove this listener at the end by:

[[NSNotificationCenter defaultCenter] removeObserver:self];

这篇关于进入前台后刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆