隐藏所有模态视图控制器

Kas*_*sem 9 iphone cocoa-touch objective-c modalviewcontroller ios

我有一个作为ModelViewController呈现的登录视图,我有一个注册视图,在其上面显示为NavigationControlloer:

登录(ModelViewController)---->注册(NavigationController)

我在Loginview上展示了Register视图(CreateAccount),如下所示:

createAccount= [[CreateAccount alloc] initWithNibName:@"CreateAccount" bundle:nil];

navController = [[UINavigationController alloc] initWithRootViewController:createAccount];

UIBarButtonItem *cancelButtun=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(HideMe)];

UIBarButtonItem *registerButtun=[[UIBarButtonItem alloc]initWithTitle:@"Register" style:UIBarButtonItemStyleBordered target:self action:@selector(Register)];

createAccount.navigationItem.leftBarButtonItem = cancelButtun;
createAccount.navigationItem.rightBarButtonItem=registerButtun;
createAccount.title=@"Create Account";

[self presentModalViewController:navController animated:YES];
Run Code Online (Sandbox Code Playgroud)

登录控制器具有NSURLConnectionDelegate用于booth登录和注册.注册完成后我只需拨打电话

[self dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

这将仅取消注册视图.

我想解雇登录视图,所以我可以回到我的主应用程序.

Sri*_*aju 23

主叫dismissModalViewController会,如果当前视图控制器没有提出任何模态控制器,调用该方法在它的父代替.在视图控制器上调用该方法将解除所有呈现的模态视图控制器到该控制器.为了显示:

如果您有三个视图控制器:vc1,vc2和vc3,vc1是主/当前使用的视图控制器.

  1. 在vc1中,您呈现模态vc2.在vc2中你然后调用dismiss,因为没有从vc2提供的模态vcs,dismiss消息被传递给父(vc1),它解散vc2,你回到了vc1.

  2. 在vc1中,你呈现模态vc2,然后从vc2呈现模态vc3.在vc3中调用dismiss会将消息发送给它的父节点(vc2),这将取消vc3.要同时关闭vc2和vc3,您需要在vc1中调用dismiss,这将关闭所有(两​​个)模态视图控制器.如果解雇动画,则只会对第一个进行动画处理.

解决此问题的最佳方法之一是使用导航控制器.即最初不是使用modalViews来呈现登录视图,而是在navigationViewcontroller那里使用.如果您需要出示注册页面.推动该视图.如果你需要转到初始视图(即除了loginView或registrationView),那么使用popToRootViewControllerAnimated方法navigationViewcontroller.

  • +1非常有用于理解vc流程. (3认同)

sup*_*ssi 20

在这里查看我对类似问题的回答:解雇ModalViewController的ModalViewController

我在我的应用程序中使用的几乎和你一样,这个解决方案适合我.请务必阅读评论,因为其中一个参考文献已经改为iOS5.

编辑: 为了解除在另一个模态视图上显示的模态视图,您必须在父项的父项上调用dismissModalViewControllerAnimated :.

iOS <5.0

[[[self parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

iOS 5.0+(必须将对parentViewController的所有引用更改为presentViewController)

[[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

  • 注意:_dismissModalViewControllerAnimated_**已弃用**(在iOS 6.0中),请使用_dismissViewControllerAnimated:completion:_. (2认同)