如何获取uinavigationbar后退按钮以返回到另一个视图控制器

时间:2012-06-21 10:58:41

标签: iphone objective-c ios uinavigationcontroller

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

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

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

感谢您的帮助。

5 个答案:

答案 0 :(得分: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];
}

答案 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];

答案 2 :(得分:1)

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

答案 3 :(得分: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];

答案 4 :(得分:1)

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

试试这个。这将导航你两次。

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

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