1

我将如何让 UINavigation 控制器导航到之前的视图而不是之前的视图。基本上我希望它跳回 2 个位置而不是默认位置。

我敢肯定,这是非常规的,但我现在只需要这样做。

 self.navigationItem.backBarButtonItem =
    [[[UIBarButtonItem alloc] initWithTitle:@"Back"
                                      style:UIBarButtonItemStyleBordered
                                     target:nil
                                     action:nil] autorelease];

谢谢你的帮助。

4

5 回答 5

4

放:

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)] autorelease];

然后创建一个方法-goBack

-(void)goBack
{
   UIViewController *ctrl = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
   [self.navigationController popToViewController:ctrl animated:YES];
}
于 2012-06-21T11:04:51.453 回答
1

如果您不想依赖数组的索引,那么您可以执行以下操作:

MyController * aMyController = nil ;
for (int i=0; i<[[self.navigationController viewControllers] count]; i++) 
{
    NSLog(@"%@",[[self.navigationController viewControllers] objectAtIndex:i]);
    if ([[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[MyController class]]) 
    {
        aMyController = [[self.navigationController viewControllers] objectAtIndex:i];      
    }
}           
[self.navigationController popToViewController:aMyController animated:YES];
于 2012-06-21T11:06:30.917 回答
1

如果您在 NavigationController 中有三个视图控制器,并且当前您正在查看第三个视图控制器并且您想跳转到第一个视图控制器。

尝试这个。这会将您导航两次。

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

在您的情况下设置适当的值而不是 0。

于 2012-06-21T11:08:45.913 回答
1

将目标添加到您添加的按钮

然后使用以下代码返回超过1个viewController

//Get the view controller that is 2 step behind
UIViewController *controller = [nav.viewControllers objectAtIndex:nav.viewControllers.count - 2];

//Go to that controller
[nav popToViewController:controller animated:YES];
于 2012-06-21T11:02:00.673 回答
1
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(youractonEvent:] autorelease];
于 2012-06-21T11:02:19.363 回答