当应用程序进入前台时重新加载应用程序数据?
我是 iPhone 开发新手。我正在构建一个应用程序,该应用程序从本地 sqlite3 数据库加载数据,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
当我点击 iPhone 按钮并将其放在后台时,然后我回想起我看到(正常)该应用程序的方式与我离开它时的方式相同。我想做的是,当它进入前台时,重新加载数据,就像从头开始调用一样。
正确的做法是什么?
提前致谢。
I'm new to iPhone development. I'm building an app that loads data from a local sqlite3 DB in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
When I tap the iPhone button and put it in the background and then I recall it I see (as normal) the app in the same way I left it. What I would like to do is, when it comes in the foreground, to reload data as if it was called from scratch.
Which is the correct way to do it?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所以在App Delegate类中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
只有在第一次进入应用程序时才会被调用。然后它会调用
- (void)applicationDidBecomeActive:(UIApplication *)application
。如果您的 iPhone iOS 是 4.0 或更高版本,当用户单击主页按钮时,它会首先调用
- (void)applicationWillResignActive:(UIApplication *)application
,然后调用- (void)applicationDidEnterBackground :(UIApplication *)应用程序
。然后应用程序将在后台运行,直到用户杀死它。当用户再次进入应用程序时,会首先调用
- (void)applicationWillEnterForeground:(UIApplication *)application
,然后调用- (void)applicationDidBecomeActive:(UIApplication *)application
。因此,与您的问题相关,您应该调用
applicationWillEnterForeground:
或applicationDidBecomeActive:
来重新加载数据。尽管在 xcode 对这些方法的注释中,Apple 建议使用applicationDidBecomeActive:
来重新启动暂停的任务和/或更新用户界面;而在applicationWillEnterForeground:
中,您可以撤消进入后台时所做的更改。因此,为了更容易查看,我为每个方法添加了数字标签。这里是当方法被调用时。
首次进入应用程序:先调用0,再调用1;
按主页按钮:呼叫 2,然后呼叫 3;
双击主页按钮(多任务处理):呼叫 2;
如果用户选择另一个应用程序或再次点击主页按钮:调用3;
如果用户再次双击主页按钮:调用 1;
再次进入应用程序:调用4,然后调用1;
So in the App Delegate class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will only be called at the first time you enter the app.Then it will call
- (void)applicationDidBecomeActive:(UIApplication *)application
.If your iphone iOS is 4.0 or later, when a user click on the home button, it will first invoke
- (void)applicationWillResignActive:(UIApplication *)application
, then- (void)applicationDidEnterBackground:(UIApplication *)application
.Then the app will run in background till the user kill it. When the user enter the app again, it will first invoke
- (void)applicationWillEnterForeground:(UIApplication *)application
, then- (void)applicationDidBecomeActive:(UIApplication *)application
.So related to your question, you should call either
applicationWillEnterForeground:
orapplicationDidBecomeActive:
to reload your data. Though in the xcode's comment of these methods, Apple suggests useapplicationDidBecomeActive:
to restart paused tasks and/or update user interface; while in theapplicationWillEnterForeground:
, you can undo the changes you've made in entering the background.So to make it easier to see, i put a number tag to each method. Here's when the method to be called.
First enter the app: call 0, then 1;
Hit home button: call 2, then 3;
Double hit home button(multitasking): call 2;
if user choose another app or hit home button again: call 3;
if user double hit home button again: call 1;
Enter app again: call 4, then 1;
在应用程序委托的上述函数中重新加载数据,以便在应用程序进入前台时刷新数据。
Reload the data in the above function of the app delegate to refresh the data when ever the app comes to foreground.
当 iOS 通知应用程序应该在后台运行时,您可以
exit()
您的应用程序,如下所示:You can
exit()
your application when iOS notifies it that it should run in the background like so:我认为这是 UIApplication 的委托:
I think that is the delegate of UIApplication: