返回 UINavigationController 的第一个索引?

发布于 2024-12-05 20:15:29 字数 667 浏览 2 评论 0原文

我正在做一个使用 UINavigationController 的应用程序,并且我正在切换到其他 UIViewController,如下所示:

if(self.myViewController == nil){
    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    self.myViewController = aViewController;
    [aViewController release];
}

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myNavController pushViewController:myViewController animated:YES];

我想这是在 UINavigationController 中创建一堆 UIViewController,也许是一个索引数组?我想知道怎样才能回头,而不需要一一回头。

例如,我正在浏览几个屏幕,并使用一个按钮,我想在第一个导航索引处返回。我还想知道如何修改索引、查看、删除以及与此问题相关的任何内容。

抱歉,如果我没有解释清楚。

I'm doing an application which uses a UINavigationController and I'm switching to other UIViewControllers as follows:

if(self.myViewController == nil){
    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    self.myViewController = aViewController;
    [aViewController release];
}

AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myNavController pushViewController:myViewController animated:YES];

I imagine this is creating a pile of UIViewControllers into the UINavigationController, maybe an array of indexs? I would like to know how to turn back without having to be back one by one.

For example, I'm sailing through a few screens and with a button I would like to return at the first index of navigation. I would also like know how to modify indexes, view, erase and anything pertaining to this issue.

Sorry if I have not explained well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

£噩梦荏苒 2024-12-12 20:15:29

你问了两个问题。

首先是,如何回到我的第一个视图控制器。正如@Thomas Clayson 和@ender 所回答的,您需要navigationcontroller 对象的popToRootViewControllerAnimated: 方法。

第二个是如何移动到视图控制器堆栈中的特定索引。答案是,您可以显式设置 viewController 数组。因此,您可以取出当前的视图控制器列表,修改它,然后将其设置回 navigationController 堆栈。它将重置堆栈并让您移动到堆栈顶部的项目。

因此:

NSMutableArray *controllers = self.navigationController.viewControllers;
[controllers removeObjectAtIndex:[controllers count] - 1]; //or whatever
[self.navigationController setViewControllers:controllers animated:YES];

You've asked two questions.

The first is, how do I get back to my first view controller. As @Thomas Clayson and @ender have answered, you want the popToRootViewControllerAnimated: method of your navigationcontroller object for that.

The second is how to move to a particular index in the view controller stack. The answer to that is, you can set the array of viewControllers explicitly. So you can pull out the current listing of view controllers, modify it, and set it back into the navigationController stack. It'll reset the stack and animate you moving to the top item in the stack.

Thusly:

NSMutableArray *controllers = self.navigationController.viewControllers;
[controllers removeObjectAtIndex:[controllers count] - 1]; //or whatever
[self.navigationController setViewControllers:controllers animated:YES];
む无字情书 2024-12-12 20:15:29
NSArray *viewControllers = [[self navigationController] viewControllers];
    for (int i = 0; i < [viewContrlls count]; i++){
        id obj = [viewControllers objectAtIndex:i];
        if ([obj isKindOfClass:[yourViewControllername class]]){
            [[self navigationController] popToViewController:obj animated:YES];
            return;
        }
    }

使用这个你可以返回到任何指定的viewController。

NSArray *viewControllers = [[self navigationController] viewControllers];
    for (int i = 0; i < [viewContrlls count]; i++){
        id obj = [viewControllers objectAtIndex:i];
        if ([obj isKindOfClass:[yourViewControllername class]]){
            [[self navigationController] popToViewController:obj animated:YES];
            return;
        }
    }

Using this you can come back to any specified viewController.

赠意 2024-12-12 20:15:29
[self.navigationController popToRootViewControllerAnimated:YES];

将带您回到第一个视图控制器(根视图控制器)。

希望这有帮助

[self.navigationController popToRootViewControllerAnimated:YES];

Will take you back to the very first view controller (root view controller).

Hope this helps

爱要勇敢去追 2024-12-12 20:15:29

用这个

NSArray *viewContrlls=[[NSArray alloc] initWithArray:[[self navigationController] viewControllers]];
    id obj=[viewContrlls objectAtIndex:1];
    [[self navigationController] popToViewController:obj animated:YES];
    [viewContrlls release];

Use this

NSArray *viewContrlls=[[NSArray alloc] initWithArray:[[self navigationController] viewControllers]];
    id obj=[viewContrlls objectAtIndex:1];
    [[self navigationController] popToViewController:obj animated:YES];
    [viewContrlls release];
舂唻埖巳落 2024-12-12 20:15:29

您应该使用 popToRootViewControllerAnimated: 来自 UINavigationController 类参考:

弹出堆栈上除根视图之外的所有视图控制器
控制器并更新显示。

You should use popToRootViewControllerAnimated: From UINavigationController class reference:

Pops all the view controllers on the stack except the root view
controller and updates the display.

清醇 2024-12-12 20:15:29

您可以返回到第一个视图,

[self.navigationController popToRootViewControllerAnimated:YES];

话虽如此,您还可以删除特定的视图控制器,或者如果您查看示例,则导航到视图控制器中的特定索引。

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack   

[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this. 

navigationController.viewControllers = allViewControllers;

You can return to the first view with

[self.navigationController popToRootViewControllerAnimated:YES];

That being said, you can also remove a particular view controller, or navigate to a specific index in your view controller if you look at the example.

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:navigationController.viewControllers];
// You can now manipulate this array with the methods used for NSMutableArray to find out / perform actions on the navigation stack   

[allViewControllers removeObjectIdenticalTo: removedViewController];
// You can remove a specific view controller with this. 

navigationController.viewControllers = allViewControllers;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文