触发applicationWillEnterForeground时更改UIView [英] Changing UIView when applicationWillEnterForeground fires

查看:123
本文介绍了触发applicationWillEnterForeground时更改UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取当用户将应用程序发送到后台时向用户显示的当前视图。

I am looking for a way to grab the current view being shown to the user when they send the application to the background.

情况是我提供的我的设置包中的选项,以便在触发applicationWillEnterForeground时我希望生效。

The situation is that I have provided options in my settings bundle for look and feel that I would like to take effect when the applicationWillEnterForeground is fired.

任何人都有关于如何解决此问题的建议?

Anyone have a suggestion as to how to attack this problem?

我试图注册一个NSNotification,但它会激活到很晚,并且在NSNotification方法运行之前,用户会看到该视图。

I tried to register a NSNotification, but it fires to late and the view appears to the user before the NSNotification method runs.

推荐答案

要获取当用户为您的应用添加背景时显示的当前视图,请使用AppDelegate的 applicationDidEnterBackground 方法。

To grab the current view being shown when the user backgrounds your app, use your AppDelegate's applicationDidEnterBackground method.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Get current view...  
    // (get from UITabbarController or whatever logic you like)

    // For this code sample, just grab first UIView under the main window
    UIView *myView = [self.window.subviews objectAtIndex:0];
}

要在显示之前更改视图,请使用AppDelegate的 applicationWillEnterForeground 这样的方法:

To change the view before it shows, use the AppDelegate's applicationWillEnterForeground method like this:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 80, 80)];
    label.backgroundColor = [UIColor whiteColor];
    label.text = @"yo";

    [self.myLastView addSubview:label];
}

我意识到你在你的问题中提到了applicationWillEnterForeground。您是否可以更具体地了解您要执行的操作或可能包含代码? 我实际上尝试了上面的代码和视图更新(我看到标签yo在我的视图上方)

I realize you mentioned the applicationWillEnterForeground in your question. Can you be more specific with what you are trying to do or possibly include code? I actually tried the code above and the view update (I see the label "yo" above my view).

我的最后一个建议是尝试致电 [view setNeedsDisplay] [view setNeedsLayout] 这应该强制UIView重绘自己。 (注意 - 问题不是uiview更改没有显示,问题它是在显示旧视图后显示的。看起来是在暂停应用程序之前拍摄的屏幕截图,此屏幕截图最初在恢复应用程序以显示时显示)

以上代码是用户看到的一个很好的例子。标签yo显示,但仅在视图恢复后显示。我会看看我能解决这个问题并重新发布。

Code above is a good example of what user is seeing. The label "yo" shows up, but only after the view is restored. I'll see if I can fix this and repost.

看来恢复暂停应用程序显示应用程序的最后已知状态的捕获屏幕截图,据我所知。我试图找到这方面的文档,但我找不到它。

It appears that resuming a suspended app shows a captured screenshot of the app's last known state as far as I can tell. I've tried to find docs to this effect, but I can't find it.

当用户点击主页按钮时,您的应用程序退出。

Have your app exit when the user hits the home button.

如果您将应用的状态保存在 applicationWillResignActive: applicationWillTerminate:,您可以简单地恢复该状态(导航到最后一个正确的标签用户)和 appDidFinishLaunchingWithOptions:您可以在用户看到它之前更新UI 。要让您的应用在背景(暂停)时退出,请输入密钥 应用程序不在后台运行 - 原始密钥:UIApplicationExitsOnSuspend

If you save the state of your app in either applicationWillResignActive: or applicationWillTerminate:, you can simply restore that state (navigate to correct tab user was last on) and in appDidFinishLaunchingWithOptions: you can update the UI before the user sees it. To have your app exit when backgrounded (suspended) put the key "Application does not run in background" - raw key: UIApplicationExitsOnSuspend to YES.

这样做可以保证用户能够按照他们在设置中配置应用的方式看到您的应用,而不会看到它曾经看起来的闪烁。对不起,我找不到更好的方法来做到这一点。 :(

Doing this will guarantee that the user will see your app the way they configured it in settings without seeing a flicker of what it used to look like. I'm sorry I couldn't find a better way to do this. :(

在您的应用中显示您的设置。

Display your settings inside your app.

这显然需要进行设计更改,但是越来越多的应用程序在应用程序中显示设置。一个受欢迎的第三方库是 在应用程序设置工具包中 。这使您可以通过bundle / plist文件配置设置,就像使用全局应用程序设置一样。它的外观和行为就像全局一样这将使您可以完全控制应用程序中的任何行为。

This obviously would require a design change, however more and more apps are showing settings inside the app. A popular third party library is In App Settings Kit. This lets you configure settings via a bundle / plist file just like you would for global app settings. It also looks and behaves just like the global settings. This will give you FULL control of any behavior you want inside your app.

这篇关于触发applicationWillEnterForeground时更改UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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