繁体   English   中英

如何获取uinavigation bar后退按钮以返回到其他视图控制器

[英]How to get a uinavigation bar back button to return to anthother view controller

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

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

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

谢谢你的帮助。

组:

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个以上的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];
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(youractonEvent:] autorelease];

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

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

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

尝试这个。 这将使您浏览两次。

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

设置适当的值而不是0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM