如何直接从第三个视图返回第一个视图?

时间:2011-09-20 02:57:28

标签: iphone

当我在第三个视图中按下取消按钮时,我想直接回到第一个视图。 我还想删除第二个视图。 我怎么能这样做?

这是代码。

// 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。 我确实添加了不推。

5 个答案:

答案 0 :(得分:3)

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];

答案 1 :(得分:2)

popToViewController:animated:是在导航控制器堆栈中弹出视图控制器时使用的UINavigationController方法。它不适合这种情况。

此用户正在添加子视图,而不是在导航控制器堆栈上推送它们。

作为一个注释,它似乎是设计问题应该使用导航控制器,第一个视图作为根控制器,然后第二个推入堆栈,第三个推进堆栈。那么你所要做的就是[self.navigationController popToRootViewControllerAnimated:YES]

我认为如果您想保留当前的架构,这将有效:

// 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类。

如果您希望在代码中创建UINavigationController,那么这也是可能的。无论您是在IB中还是在代码中的nib中创建UINavigationController,我都会在下面显示您需要的其他代码。

我还建议您阅读View Controller Programming Guide for iOS

在您的app代理或其他代码中:

-(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];
}

在第一个视图控制器中:

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

在第二个视图控制器中:

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

在第三个视图控制器中:

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

答案 2 :(得分:0)

使用

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

返回特定的视图控制器。

答案 3 :(得分:0)

试试这个:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

这将弹出到索引1处的视图。希望帮助!

答案 4 :(得分:0)

// this part is in the third view.
- (void)cancel {
    self.first = [SecondController alloc] init];
    [self.view addSubview:second.view];
}

我认为,如果你不需要担心删除视图,以后会删除。