objective-c - 当应用程序在ios上进入后台时如何关闭模态视图

标签 objective-c ios modalviewcontroller

我在方法中创建了一个模态视图(主视图中没有引用),我想在我的应用程序进入后台时自动执行 dismissModalViewControllerAnimated。我怎样才能做到这一点 ?

最佳答案

在主视图的 viewDidLoad 中,添加观察者以在应用进入后台时收到通知。

- (void) viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(goToBackground) 
        name:UIApplicationWillResignActiveNotification object:nil];
}

定义函数 goToBackground()。当应用程序进入后台时将调用它

- (void) goToBackground
{
    [self dismissModalViewControllerAnimated: NO]; // no need to animate 
}

别忘了移除观察者

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

关于objective-c - 当应用程序在ios上进入后台时如何关闭模态视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9516472/

相关文章:

ios - PresentModalViewController 不会停止工作流

ios - 后退按钮未出现在 View Controller 中

iphone - iOS 标签栏不显示模态视图

ios - 在 Swift 中访问 C 宏

iphone - 向 UITableViewCell 添加动态图像

ios - Apple 内购 transaction_id 一次购买有时会变化

php - iOS 推送通知无法使用 PHP

ios - 弹出窗口可见时禁用所有交互

objective-c - 在 UITableviewCell 高度动画的同时动画 CALayer 阴影

ios - 在 MKMapView 中覆盖城市、地区等