iphone - 当应用程序进入后台时关闭 modalviewcontroller

标签 iphone background modalviewcontroller

当应用程序进入后台时,我需要自动关闭我的uiimagepicker模态视图 Controller 。我尝试将代码放在viewdiddissappear方法中的dismissmodalviewcontroller代码中,但它没有被调用。所以我在appdelegate中引用了 View Controller 并尝试将其放入 applicationdidenterbackground 方法中,但它仍然不起作用。有人可以指出执行此操作的正确方法

最佳答案

尝试在要关闭的 UIViewController 中为 UIApplicationDidEnterBackgroundNotification 添加 NSNotificationCenter 观察者。使用选择器关闭模态视图

- (void)viewWillAppear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(didEnterBackground:) 
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter] removeObserver: self
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];
}

- (void)didEnterBackground:(NSNotification*)note
{
  [self.navigationController dismissModalViewAnimated:NO];
}

关于iphone - 当应用程序进入后台时关闭 modalviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5685419/

相关文章:

iphone - SCNetworkReachability编译错误

iphone - 将 indexpath 行传回父 View

objective-c - 从 UISplitViewController 呈现的 modalViewController 出现错误的方向

ios8 - 模态视图 Controller 被解除后iOS 8键盘解除延迟

ios - Xcode:如果您的项目不是 View Controller 项目,您可以更改 View 吗?

iphone - 如何从 xcode 中的 nib 文件创建 View ?

iphone - 扩展教程 Material 时 entityForName 和 ManagedObjectContext 的问题

swift - WKRefreshBackgroundTask cleanupStorage 尝试访问文件时出错

background - NSURLSession:通过后台传输上传 Assets

html - 如何保持图像背景图像静止?