在后台关闭模态视图控制器
Dimiss modal View Controller in background
我得到了View A present -> Modal View B, B present -> Modal View C Modal View D -> Modal View E
。是一个主ViewA然后继承Modal视图。
所以每次我呈现新模态 C 或 D 我想关闭前一个(这样当我关闭新呈现的模态时它会显示主视图A总是不是以前的模态)。
- 如果我在B,我想展示C,那么我必须先展示C
然后在后台我解雇 B.
- 如果我在 C 并且我想展示 E 那么我先展示 E 然后再展示 E
在后台关闭 C
我试过这段代码:
if (_openNextView) {
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]) {
NSLog(@"generated Code %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]);
NSLog(@"phone Number %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"phoneNumber"]);
// Present C View
RegisterSecondViewController *registerSecond = [[RegisterSecondViewController alloc] initWithNibName:@"RegisterSecondViewController" bundle:nil];
//[self presentNatGeoViewController:registerSecond];
[self presentViewController:registerSecond animated:YES completion:nil];
} else {
RegisterFirstViewController *registerFirst = [[RegisterFirstViewController alloc] initWithNibName:@"RegisterFirstViewController" bundle:nil];
//present D view
//[self presentNatGeoViewController:registerFirst];
[self presentViewController:registerFirst animated:YES completion:nil];
}
// Dismiss privious View (the current view before presenting new one)
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
此外,我还在新呈现的模态视图中添加了这段代码
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
但没有奏效并且视图未被驳回
UIViewController
有一个名为 presentedViewController
的 属性 :
presentedViewController
Property The view controller that is
presented by this view controller, or one of its ancestors in the view
controller hierarchy. (read-only)
这意味着,您在给定时间只能展示一个模态控制器。为了展示另一个,你需要先关闭当前的。
如果您想要自定义导航,我建议您创建一个自定义容器视图控制器,因为正常的模式表示不适合您的用例。 Here 是 Apple 的指南。
您可以在呈现新的 vc 之后关闭当前的 vc。试试这个:
[self presentViewController:vc animated:YES completion:^{
if (self.presentingViewController != nil) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:NO completion:nil];
});
}
}];
我会模态地呈现一个 UINavigationController
并用它来呈现内容视图。然后,当您当前呈现新的模态视图时,您只需设置导航控制器的 viewControllers
(动画)。
这将为您提供整个设置过程的动画视图,但不允许用户返回并会在 VC 完成后解除分配。
我得到了View A present -> Modal View B, B present -> Modal View C Modal View D -> Modal View E
。是一个主ViewA然后继承Modal视图。
所以每次我呈现新模态 C 或 D 我想关闭前一个(这样当我关闭新呈现的模态时它会显示主视图A总是不是以前的模态)。
- 如果我在B,我想展示C,那么我必须先展示C 然后在后台我解雇 B.
- 如果我在 C 并且我想展示 E 那么我先展示 E 然后再展示 E 在后台关闭 C
我试过这段代码:
if (_openNextView) {
if ([[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]) {
NSLog(@"generated Code %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"generatedCode"]);
NSLog(@"phone Number %@ : ", [[NSUserDefaults standardUserDefaults] stringForKey:@"phoneNumber"]);
// Present C View
RegisterSecondViewController *registerSecond = [[RegisterSecondViewController alloc] initWithNibName:@"RegisterSecondViewController" bundle:nil];
//[self presentNatGeoViewController:registerSecond];
[self presentViewController:registerSecond animated:YES completion:nil];
} else {
RegisterFirstViewController *registerFirst = [[RegisterFirstViewController alloc] initWithNibName:@"RegisterFirstViewController" bundle:nil];
//present D view
//[self presentNatGeoViewController:registerFirst];
[self presentViewController:registerFirst animated:YES completion:nil];
}
// Dismiss privious View (the current view before presenting new one)
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
此外,我还在新呈现的模态视图中添加了这段代码
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
但没有奏效并且视图未被驳回
UIViewController
有一个名为 presentedViewController
的 属性 :
presentedViewController
Property The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy. (read-only)
这意味着,您在给定时间只能展示一个模态控制器。为了展示另一个,你需要先关闭当前的。
如果您想要自定义导航,我建议您创建一个自定义容器视图控制器,因为正常的模式表示不适合您的用例。 Here 是 Apple 的指南。
您可以在呈现新的 vc 之后关闭当前的 vc。试试这个:
[self presentViewController:vc animated:YES completion:^{
if (self.presentingViewController != nil) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:NO completion:nil];
});
}
}];
我会模态地呈现一个 UINavigationController
并用它来呈现内容视图。然后,当您当前呈现新的模态视图时,您只需设置导航控制器的 viewControllers
(动画)。
这将为您提供整个设置过程的动画视图,但不允许用户返回并会在 VC 完成后解除分配。