我在方法中创建了一个模态视图(主视图中没有引用),我想在我的应用程序进入后台时自动执行 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/