iPhone模态视图-如何返回父父控制器

发布于 2024-11-16 16:53:51 字数 1533 浏览 1 评论 0原文

我有几个以模态方式依次打开的视图。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

廻憶裏菂餘溫 2024-11-23 16:53:51

为什么不使用UINavigationController?您可以同时使用 popToRootViewControllerAnimated:popViewControllerAnimated: 来达到您的目的。

因此,如果您这样做,

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

您应该返回1

摘自 dismissModalViewControllerAnimated

如果您连续呈现多个模态视图控制器,从而构建一个模态视图控制器堆栈,在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈中该子级之上的所有视图控制器。发生这种情况时,只有最顶层的视图会以动画方式消失;任何中间视图控制器都会从堆栈中删除。最顶层的视图使用其模态转换样式关闭,该样式可能与堆栈中较低的其他视图控制器使用的样式不同。

使用导航控制器

为了使导航控制器正常工作,请改为您将在加载 view1 的位置执行此操作,

View1 *myView = [[[View1 alloc] initWithNibName:@"View1" bundle:nil] autorelease]; 
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease]

[mainNavController presentModalViewController:navController animated:YES];

这是假设 view1rootViewController

一旦您设置好导航控制器后就可以加载view2 像这样,

View2 *myView = [[View2 alloc] initWithNibName:@"View2" bundle:[NSBundle mainBundle]];   
[self.navigationController pushViewController:myView animated:YES];
[myView release];

在这种情况下,

取消

[self.navigationController popViewControllerAnimated:YES];

完成

[self.navigationController popToRootViewControllerAnimated:YES];

Why don't you use UINavigationController? You can use both popToRootViewControllerAnimated: and popViewControllerAnimated: for your purpose.

As such if you do,

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

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,

View1 *myView = [[[View1 alloc] initWithNibName:@"View1" bundle:nil] autorelease]; 
UINavigationController * navController = [[[UINavigationController alloc] initWithRootViewController:myView] autorelease]

[mainNavController presentModalViewController:navController animated:YES];

This is assuming that view1 was the rootViewController

Once you've the navigation controller set up then you can load view2 like this,

View2 *myView = [[View2 alloc] initWithNibName:@"View2" bundle:[NSBundle mainBundle]];   
[self.navigationController pushViewController:myView animated:YES];
[myView release];

In such case,

Cancel

[self.navigationController popViewControllerAnimated:YES];

Complete

[self.navigationController popToRootViewControllerAnimated:YES];
醉态萌生 2024-11-23 16:53:51

似乎没有聪明的方法,试试这个愚蠢的方法:

  UIViewController *vc = self;
  while(vc.parentViewController.modalViewController == vc){
    [[vc retain] autorelease];
    [vc dismissModalViewControllerAnimated:NO];
    vc = vc.parentViewController;
  }

Seems there is no smart way, try this stupid one:

  UIViewController *vc = self;
  while(vc.parentViewController.modalViewController == vc){
    [[vc retain] autorelease];
    [vc dismissModalViewControllerAnimated:NO];
    vc = vc.parentViewController;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文