如何直接从第三视图返回第一视图? [英] How can I go back to the first view from the third view directly?

查看:49
本文介绍了如何直接从第三视图返回第一视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在第三个视图中按取消"按钮时,我想直接回到第一个视图.我也想删除第二个视图.我该怎么办?

When I push cancel button in the third view, I want to go back to the first view directly. I also want to remove the second view. How can I do that?

这是代码.

// this part is in the first view.
self.second = [SecondController alloc] init];
[self.view addSubview:second.view];

// this part is in the second view.
ThirdController *thirdController = [[ThirdController alloc] initWithStyle:UITableViewStyleGrouped];             
self.navigationController = [UINavigationController alloc] initWithRootViewController:thirdController];
[self.view addSubview:navigationController.view];

// this part is in the third view.
- (void)cancel {
    [self.view removeFromSuperview]; // this only goes to the second view.
}

我可以在contoller中使用popToViewController吗?我的应用程序崩溃了.我以为popToViewController只能在调用控制器中使用.当popToViewController被推入时使用.我没有添加不推送.

Can I use popToViewController in called contoller? My app crashes. I thought popToViewController can be used only in calling controller. And popToViewController is used when it was pushed. I did add not push.

推荐答案

popToViewController:animated:是一种 UINavigationController 方法,在将视图控制器从导航控制器弹出时使用堆.它不适合这种情况.

popToViewController:animated: is a UINavigationController method that you use when popping view controllers off the navigation controller stack. It doesn't fit for this scenario.

此用户正在添加子视图,而不是将其推入导航控制器堆栈中.

This user is adding subviews, not pushing them on a navigation controller stack.

请注意,在设计上,您应该使用导航控制器,将第一个视图用作根控制器,然后将第二个视图推入堆栈,然后将第三个推入堆栈堆栈.然后,您要做的就是 [self.navigationController popToRootViewControllerAnimated:YES] .

As a note, it appears as a matter of design you should be using a navigation controller with the first view as the root controller, then the second pushed on the stack, and the third pushed on the stack. Then all you have to do is [self.navigationController popToRootViewControllerAnimated:YES].

如果您希望保留当前的体系结构,我认为这会起作用:

I think this will work if you want to keep your current architecture:

// this part is in the third view.
- (void)cancel {
    // remove the second view (self.view.superview) from the first view
    [self.view.superview removeFromSuperView];
    // can't recall, possibly you still need to remove the third view, but i think removing the superview will do it.
    // [self.view removeFromSuperView];
}


如果您想尝试 UINavigationController 路线,那么最简单的路径是在Xcode中创建一个新项目,并为基于导航的应用程序或主从应用程序选择类型.这将在笔尖中创建一个 UINavigationController 并将其添加到您的窗口中.然后,您可以在Interface Builder中将根视图控制器设置为FirstViewController类.


If you prefer to try the UINavigationController route, then the easiest path is to create a new project in Xcode and select the type for a Navigation-Based Application or a Master-Detail Application. This will create a UINavigationController in a nib and add it to your window. You can then set the root view controller in Interface Builder to your FirstViewController class.

如果您更喜欢用代码创建 UINavigationController ,那么这也是可能的.我在下面说明了这一点,以及您需要的其余所有代码,无论您是在IB的笔尖中还是在代码中创建 UINavigationController .

If you prefer to create the UINavigationController in code, then that is also possible. I show that below, along with the rest of the code you need, regardless of whether you create your UINavigationController in a nib in IB or in code.

我还建议您阅读适用于iOS的《 View Controller编程指南》.

在您的应用程序委托中或其他一些代码中:

In your app delegate or some other code:

-(void)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions [
    // I would recommend setting up the UINavigationController and FirstViewController as IBOutlets in your nib, but it can be done in code.
    FirstViewController* fvc = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:fvc];

    [window addSubView:navController.view];
    [window makeKeyAndVisible];

    [fvc release];
    [navController release];
}

在第一个视图控制器中:

In the first view controller:

SecondViewController* svc = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
[self.navigationController pushViewController:svc animated:YES];
[svc release];

在第二个视图控制器中:

In the second view controller:

ThirdViewController* tvc = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
[self.navigationController pushViewController:tvc animated:YES];
[tvc release];

在第三个视图控制器中:

In the third view controller:

-(void)cancel {
    // returns to the first view controller
    [self.navigationController popToRootViewControllerAnimated:YES];
}

这篇关于如何直接从第三视图返回第一视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆