Reputation: 14420
I am a newbie in iOS...
In my project i am navigating to other views using addsubview
...
I have many views say 1 to 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..
I am using UINavigationController example, but as far as i understood we can only go to the previous view...
I have used another example in which it takes me to the root view
..
following is the code
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
So should i use a UINavigationController or do i need to use the above method with modifications??
Any ideas how can i achieve this..
Please Help!!!!
Thanks.
I am not using navigation controller currently but instead using addsubview method... Is this a bad practice, do i specifically need to navigation control.. Thanks!!!
Upvotes: 0
Views: 209
Reputation: 2246
Sweet and Simple Solution : Just Write below code where your function is called:
- (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.
}
Hope this might help you.
Upvotes: 0
Reputation: 374
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];
}
}
Upvotes: 0
Reputation: 1376
Please try following,
//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];
}
}
Thanks,
Upvotes: 0
Reputation: 3399
You can use the following method of a navigation controller to move to a particular view:
popToViewController:animated:
See here for docs.
So if you have a UINavigationController *navCon
and want to move to the second view use the below code:
UIViewController *vc = [navCon.viewControllers objectAtIndex:1];
[navCon popToViewController:vc animated:YES];
Upvotes: 3
Reputation: 6445
If you want to go to the 2nd view from 5th view use the below code:
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[allViewControllers removeObject:self];//removing 5th
[allViewControllers removeLastObject];//removing 4th
[allViewControllers removeLastObject];//removing 3rd
self.navigationController.viewControllers = allViewControllers;
Now just pop the navigation controller
Upvotes: 1