在我的项目中,我在导航控制器中添加了一些视图控制器。
UIViewController1,UIViewController2,UIViewController3,UIViewController4,UIViewController5
考虑UIViewController1
是导航控制器的根视图控制器。点击按钮后导航到达UIViewContoller5
后,我需要返回UIViewController1
。所以我正在编写以下代码。
- (void)popToRootViewControllerAnimated
{
NSLog(@"%@",[self.navigationController viewControllers]);
[self.navigationController popToRootViewControllerAnimated:NO];
}
在控制台中,它打印像
(
"<UIViewController1: 0x8e3fcc0>",
"<UIViewController2: 0x9a5d310>",
"<UIViewController3: 0x9a67b00>",
"<UIViewController4: 0x9162a00>",
"<UIViewController5: 0x9a84380>"
)
但在完成执行后,我的观点仍然在UIViewController3
。如果我在[self.navigationController viewControllers]
中打印UIViewController3
,则显示为
(
"<UIViewController1: 0x8e3fcc0>",
"<UIViewController3: 0x9a67b00>",
)
我缺少什么。提前致谢。任何帮助,将不胜感激。
答案 0 :(得分:3)
试试这个:
UIViewController *firstVc = [viewControllers objectAtIndex:0];
[navCtrl setViewControllers:@[firstVc] animated:NO];
答案 1 :(得分:1)
试试这个希望这会解决你的问题
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
这个控制器带你到所有控制器的零(0)索引。 试试这个
UIViewController *ctrl = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count -1];
[self.navigationController popToViewController:ctrl animated:YES];
希望这能解决您的问题。
答案 2 :(得分:0)
你试过这个吗?
-(void)backButtonAction
{
YourAppDelegate *app=(YourAppDelegate *)[[UIApplication sharedApplication]delegate];
for(UIViewController *vc in app.yourNavigationController.viewControllers)
{
if([vc isKindOfClass:[UIViewController1 class]])
{
[app.yourNavigationController popToViewController:vc animated:YES];
}
}
}
答案 3 :(得分:0)
此解决方案有效
for(UIViewController *objUIViewController in [self.navigationController viewControllers])
{
if([objUIViewController isKindOfClass:[UIViewController1 class]])
{
[self.navigationController setViewControllers:[NSArray arrayWithObject:objUIViewController]animated:YES];
}
}