如果我将我的应用程序移至后台然后在 iOS 中进入前台,则呈现的视图控制器将被关闭
Presented view controller gets dismissed if i move my application to background and then come to foreground in iOS
当用户将应用程序移至后台时,我需要用黑色布局覆盖屏幕 applicationDidEnterBackground 以维护屏幕上某些敏感数据的隐私。因此,为此我使用 AppDelegate 函数在背景中呈现黑色,然后在出现在前景时通过将其关闭来移除它 applicationDidEnterBackground。代码是:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIViewController *blankViewController = [UIViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.window.rootViewController dismissViewControllerAnimated:false completion:nil];
}
@end
现在在我的应用程序中一切正常,但在一个屏幕上,我在单击按钮时显示 ViewContollerB 使用:[self presentViewController:webview animated:YES completion:nil];
应用程序通常会被覆盖移动到后台时为黑色,但是当我在此之后将应用程序带到前台时,显示的 ViewContollerB 也被取消。如何防止我的 ViewController 在后台出现时被解雇?
您正在尝试执行以下操作:rootviewcontroller A 显示空白 viewcontroller B。您的应用程序进入后台,然后您从那里显示 viewcontroller C。现在 B 关闭,因为 A 同时展示 B 和 C,这是不允许的。
您将需要检查您的根viewcontroller 是否呈现任何viewcontroller,以及它们是否递归呈现任何其他。您可以在 viewcontroller.
上使用 属性 presentedViewController
检查这些
就我个人而言,我会(在所有 viewcontroller 继承自的基础 viewcontroller class 中)保留一个变量来检查这个变量是否可见(通过跟踪 viewDidAppear
和 viewDidDisappear
)。如果这是可见的,则在顶部添加一个空白视图。
在 Swift 中回答,因为没有编辑,Objective-C 不能信任我:
class BaseViewController: UIViewController {
var appeared = false
func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated: animated)
appeared = true
}
func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated: animated)
appeared = false
}
}
然后你需要从 AppDelegate
触发一个通知,它被捕获在这个 viewcontroller 中,然后在顶部显示一个空白视图。
创建一个名为 OverLayViewController
的新 'UIViewController' 并将其加载到
applicationDidEnterBackground
方法
- (void)applicationDidEnterBackground:(UIApplication *)application {
OverLayViewController *blankViewController = [OverLayViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
UIViewController *rvc = self.window.rootViewController;
UIViewController *pvc = rvc.presentedViewController; // you may need to loop through presentedViewControllers if you have more than one
if(pvc != nil) {
[pvc presentViewController: blankViewController animated: NO completion:nil];
}
else{
[self.window.rootViewController presentViewController: blankViewController animated: NO completion:nil];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
UIViewController *test = [self topViewController];
if ([test isKindOfClass:[OverLayViewController class]]) {
[test dismissViewControllerAnimated:false completion:nil];
}
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
-(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
在这种情况下,dismissViewContoller
代码不会应用于您的网络视图。
当用户将应用程序移至后台时,我需要用黑色布局覆盖屏幕 applicationDidEnterBackground 以维护屏幕上某些敏感数据的隐私。因此,为此我使用 AppDelegate 函数在背景中呈现黑色,然后在出现在前景时通过将其关闭来移除它 applicationDidEnterBackground。代码是:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIViewController *blankViewController = [UIViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:blankViewController animated:NO completion:NULL];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[self.window.rootViewController dismissViewControllerAnimated:false completion:nil];
}
@end
现在在我的应用程序中一切正常,但在一个屏幕上,我在单击按钮时显示 ViewContollerB 使用:[self presentViewController:webview animated:YES completion:nil];
应用程序通常会被覆盖移动到后台时为黑色,但是当我在此之后将应用程序带到前台时,显示的 ViewContollerB 也被取消。如何防止我的 ViewController 在后台出现时被解雇?
您正在尝试执行以下操作:rootviewcontroller A 显示空白 viewcontroller B。您的应用程序进入后台,然后您从那里显示 viewcontroller C。现在 B 关闭,因为 A 同时展示 B 和 C,这是不允许的。
您将需要检查您的根viewcontroller 是否呈现任何viewcontroller,以及它们是否递归呈现任何其他。您可以在 viewcontroller.
上使用 属性presentedViewController
检查这些
就我个人而言,我会(在所有 viewcontroller 继承自的基础 viewcontroller class 中)保留一个变量来检查这个变量是否可见(通过跟踪 viewDidAppear
和 viewDidDisappear
)。如果这是可见的,则在顶部添加一个空白视图。
在 Swift 中回答,因为没有编辑,Objective-C 不能信任我:
class BaseViewController: UIViewController {
var appeared = false
func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated: animated)
appeared = true
}
func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated: animated)
appeared = false
}
}
然后你需要从 AppDelegate
触发一个通知,它被捕获在这个 viewcontroller 中,然后在顶部显示一个空白视图。
创建一个名为 OverLayViewController
的新 'UIViewController' 并将其加载到
applicationDidEnterBackground
方法
- (void)applicationDidEnterBackground:(UIApplication *)application {
OverLayViewController *blankViewController = [OverLayViewController new];
blankViewController.view.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
UIViewController *rvc = self.window.rootViewController;
UIViewController *pvc = rvc.presentedViewController; // you may need to loop through presentedViewControllers if you have more than one
if(pvc != nil) {
[pvc presentViewController: blankViewController animated: NO completion:nil];
}
else{
[self.window.rootViewController presentViewController: blankViewController animated: NO completion:nil];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
UIViewController *test = [self topViewController];
if ([test isKindOfClass:[OverLayViewController class]]) {
[test dismissViewControllerAnimated:false completion:nil];
}
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
-(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
在这种情况下,dismissViewContoller
代码不会应用于您的网络视图。