我是iOS的新手......
在我的项目中,我使用addsubview
...
我有很多观点说1到7 ..
What i want is when i am on the 7th view or 6th view or 5th view i want to go back to the 2nd view on a button click..
我正在使用 UINavigationController 示例,但据我所知,我们只能转到上一个视图......
我使用了另一个例子,它带我去了root view
..
以下是代码
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
我应该使用 UINavigationController 还是需要使用上述方法进行修改?
任何想法我怎样才能做到这一点..
请帮助!!!!
感谢。
我目前没有使用导航控制器,而是使用addsubview方法...... 这是一个不好的做法,我特别需要导航控制.. 感谢!!!
答案 0 :(得分:3)
您可以使用导航控制器的以下方法移动到特定视图:
popToViewController:animated:
有关文档,请参阅here。
因此,如果您有UINavigationController *navCon
并希望转到第二个视图,请使用以下代码:
UIViewController *vc = [navCon.viewControllers objectAtIndex:1];
[navCon popToViewController:vc animated:YES];
答案 1 :(得分:1)
如果您想从第5个视图转到第2个视图,请使用以下代码:
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[allViewControllers removeObject:self];//removing 5th
[allViewControllers removeLastObject];//removing 4th
[allViewControllers removeLastObject];//removing 3rd
self.navigationController.viewControllers = allViewControllers;
现在只需弹出导航控制器
答案 2 :(得分:0)
请尝试以下,
//This for loop iterates through all the view controllers in navigation stack.
for (UIViewController* viewController in self.navigationController.viewControllers) {
//This if condition checks whether the viewController's class is MyViewController
// if true that means its the MyViewController (which has been pushed at some point)
if ([viewController isKindOfClass:[MyViewController class]] ) {
// Here viewController is a reference of UIViewController base class of MyViewController
// but viewController holds MyViewController object so we can type cast it here
MyViewController *ObjmyViewController = (MyViewController*)viewController;
[self.navigationController popToViewController:ObjmyViewController animated:YES];
}
}
谢谢,
答案 3 :(得分:0)
for (UIViewController *viewcontroller in self.navigationController.viewControllers)
{
if ([viewcontroller isKindOfClass:[self.navigationController class]])
{
UIStoryboard *story_profile=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
FindFriends_ViewController *objc_prof = [story_profile instantiateViewControllerWithIdentifier:@"FindFriends_ViewController"];
[self.navigationController popToViewController:objc_prof animated:YES];
}
}
答案 4 :(得分:0)
甜蜜而简单的解决方案: 只需在下面的代码中写下您的函数调用:
- (IBAction)goToSecondViewController
{
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
// where i indicates viewController number,In your case write 2 instead of i.
}
希望这对你有所帮助。