iPhone模态视图-如何返回父父控制器
我有几个以模态方式依次打开的视图。 View1 调用 View2,View2 调用 View3。
我使用此代码来调用下一个视图:
View2 *myView = [[View2 alloc] initWithNibName:@"View2" bundle:[NSBundle mainBundle]];
[self presentModalViewController:myView animated:YES];
[myView release];
如果用户按下取消,那么它会返回一个视图... 3 到 2 和 2 到 1
[self.parentViewController dismissModalViewControllerAnimated:YES];
我需要做的是,当用户位于 View3 上时,如果他们不选择取消而是完成操作,那么我需要回到View1并释放View2和View3。
我该怎么做?
编辑:主窗口有一个导航控制器和 6 个视图控制器。我这样称呼视图 1:
View1 *screen = [[View1 alloc] initWithNibName:@"View1" bundle:[NSBundle mainBundle]];
self.Search = screen;
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
编辑#2: 主窗口调用视图 1。主窗口在 XIB 中有一个 NavController,这是有效的:
View1 *screen = [[View1 alloc] initWithNibName:@"View1" bundle:[NSBundle mainBundle]];
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
然后在视图 1 上的 XIB 中,我添加了一个 NavController 并将其绑定到 .h 中的 View1NavController,
视图 1 然后调用视图 2:
View2 *myView = [[[View2 alloc] initWithNibName:@"View2" bundle:nil] autorelease];
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease];
[View1NavController presentModalViewController:navController animated:YES];
当我执行这个,没有错误,但它不显示View2。
I have several views that I open one after another modally. View1 calls View2 and View2 calls View3.
I use this code to call the next view:
View2 *myView = [[View2 alloc] initWithNibName:@"View2" bundle:[NSBundle mainBundle]];
[self presentModalViewController:myView animated:YES];
[myView release];
If the user pushes cancel then it goes back one View… 3 to 2 and 2 to 1
[self.parentViewController dismissModalViewControllerAnimated:YES];
What I need to do is when the user is on View3 if they don’t select cancel but complete the operation, then I need to go back to View1 and release View2 and View3.
How do I do that?
EDIT: The MAIN WINDOW has a Navcontroller and 6 view controllers. I call the View 1 like this:
View1 *screen = [[View1 alloc] initWithNibName:@"View1" bundle:[NSBundle mainBundle]];
self.Search = screen;
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
EDIT #2:
Main Windows calls View 1. Main Window has a NavController in the XIB this works:
View1 *screen = [[View1 alloc] initWithNibName:@"View1" bundle:[NSBundle mainBundle]];
[mainNavController presentModalViewController:screen animated:YES];
[screen release];
Then in the XIB on View 1 I added a NavController and tied it to View1NavController in the .h
View 1 then calls view 2:
View2 *myView = [[[View2 alloc] initWithNibName:@"View2" bundle:nil] autorelease];
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease];
[View1NavController presentModalViewController:navController animated:YES];
When I execute this, no errors, but it doesnt show the View2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用
UINavigationController
?您可以同时使用popToRootViewControllerAnimated:
和popViewControllerAnimated:
来达到您的目的。因此,如果您这样做,
您应该返回
1
。摘自
dismissModalViewControllerAnimated
,如果您连续呈现多个模态视图控制器,从而构建一个模态视图控制器堆栈,在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈中该子级之上的所有视图控制器。发生这种情况时,只有最顶层的视图会以动画方式消失;任何中间视图控制器都会从堆栈中删除。最顶层的视图使用其模态转换样式关闭,该样式可能与堆栈中较低的其他视图控制器使用的样式不同。
使用导航控制器
为了使导航控制器正常工作,请改为您将在加载
view1
的位置执行此操作,这是假设
view1
是rootViewController
一旦您设置好导航控制器后就可以加载
view2
像这样,在这种情况下,
取消
完成
Why don't you use
UINavigationController
? You can use bothpopToRootViewControllerAnimated:
andpopViewControllerAnimated:
for your purpose.As such if you do,
You should go back to
1
.Excerpt from
dismissModalViewControllerAnimated
,If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
Using Navigation Controller
For navigation controller to work, instead of where your load your
view1
you will do this,This is assuming that
view1
was therootViewController
Once you've the navigation controller set up then you can load
view2
like this,In such case,
Cancel
Complete
似乎没有聪明的方法,试试这个愚蠢的方法:
Seems there is no smart way, try this stupid one: