Pop To RootView Controller issue - ios

In my project I have some set of view controller added in a navigation controller like.
UIViewController1,UIViewController2,UIViewController3,UIViewController4,UIViewController5
consider UIViewController1 is my root view controller of navigation controller. after the navigation reaches UIViewContoller5 on button click I need to return back to my UIViewController1. So I'm writing following code.
- (void)popToRootViewControllerAnimated
{
NSLog(#"%#",[self.navigationController viewControllers]);
[self.navigationController popToRootViewControllerAnimated:NO];
}
In console it prints like
(
"<UIViewController1: 0x8e3fcc0>",
"<UIViewController2: 0x9a5d310>",
"<UIViewController3: 0x9a67b00>",
"<UIViewController4: 0x9162a00>",
"<UIViewController5: 0x9a84380>"
)
But after it finishes execution my view stays at UIViewController3. If I print [self.navigationController viewControllers] in my UIViewController3 it shows like,
(
"<UIViewController1: 0x8e3fcc0>",
"<UIViewController3: 0x9a67b00>",
)
What I m missing. Thanks in advance. Any help would be appreciated.

Try this:
UIViewController *firstVc = [viewControllers objectAtIndex:0];
[navCtrl setViewControllers:#[firstVc] animated:NO];

Try this hope this will solve your problem
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
this controller take you at zero(0) index of you all controllers.
Try this one
UIViewController *ctrl = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count -1];
[self.navigationController popToViewController:ctrl animated:YES];
Hope this will solve your issue.

Have you tried this.
-(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];
}
}
}

This solution works
for(UIViewController *objUIViewController in [self.navigationController viewControllers])
{
if([objUIViewController isKindOfClass:[UIViewController1 class]])
{
[self.navigationController setViewControllers:[NSArray arrayWithObject:objUIViewController]animated:YES];
}
}

Related

Pop to particular controller on iOS | Objective-c

I have the following code to push new ViewControllers.
- (IBAction)btnEditPressed:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"contactListViewController"];
[self parentShowViewController:controller sender:sender];
}
- (void)parentShowViewController:(UIViewController *)controller sender:(id)sender {
if ([self isIOS7]) {
// En iOS7 no existe el método showViewController
[self.navigationController pushViewController:controller animated:YES];
} else {
//[self.navigationController pushViewController:controller animated:YES];
[super showViewController:controller sender:sender];
}
}
Now I have the following scenario: I have 3 ViewControllers called A,B,C.
A->B->C If press back button I want to back from C to A
Try something like this
If you want go to Controller A from controller C on Controller C create custom back button and set the action of it, and put the following code.
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
This is only work if you know that Controller A is your first Controller in Navigation.
If you don't know the order of viewController try this one
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewControllerA class]]) {
[self.navigationController popToViewController:VC animated:Yes];
}
}
And to add a custom button go to this link
Hope this will help you.

How t go at specific page in ios

I know this question have been answered before, but i have difficulty to pop on specific page in ios. As i have already tried many times. as i have build 4 pages and and want to move last page to second page.
with the help of this code i have gone through first page
[self.navigationController popToRootViewControllerAnimated:YES];
and with the help of this page i have gone to 3rd page.
[self.navigationController popViewControllerAnimated:YES];
but for second page i have written this code
but it's not working
ViewPage2 *ptwo=[[ViewPage2 alloc] initWithNibName:#"ViewPage2" bundle:nil];
[self.navigationController popToViewController:ptwo animated:YES];
It is because you create new view controller which is not in the stack. What you should do is following:
for (UIViewController *vc in self.navigationController.viewControllers) {
if ( [vc isKindOfClass:ViewPage2]){
[self.navigationController popToViewController:vc animated:true];
return; //optional
}
}
ViewPage2 *ptwo;
for (UIViewController *vc in self.navigationController.viewControllers) {
if ([vc isKindOfClass:[ViewPage2 class]]) {
ptwo = (ViewPage2 *)vc;
break;
}
}
[self.navigationController popToViewController:ptwo animated:true];
Here is the simple answer:
NSArray *arr = [self.navigationController viewControllers];
[self.navigationController popToViewController:[arr objectAtIndex:0] animated:YES];
In place of,
[arr objectAtIndex:0]
here you can give index of specified view controller that you need to pop.
You can try doing this
first import the ViewController on which page u want to go.
like
#import ViewController.h
Then u can do the following
instantiate the class
ViewController *controller = [[ViewController alloc ] init];
[self.navigationController pushViewController:controller animated:YES];
or you can do popToViewController too
for (UIViewController *controller in self.navigationController.viewControllers) {
//Do not forget to import YourViewController.h
if ([controller isKindOfClass:[ViewController class]]) {
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}

Problems with using popToViewController to go back to a specific view controller going to black screen

I am using doing something basic like this to push to a view.
UIStoryboard *storyboard = self.navigationController.storyboard;
MenuViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"menuViewController"];
[self.navigationController pushViewController:viewController animated:YES];
Some places I can simply use:
[self.navigationController popViewControllerAnimated:YES];
But in a few places I want to pop to a specific view controller. What I have tried is:
UIStoryboard *storyboard = self.navigationController.storyboard;
RecordMenuViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"recordMenuViewController"];
[self.navigationController popToViewController:viewController animated:YES];
But it just goes to a black screen like the view isn't in the stack or something. What am I missing here?
If you want to pop to a specific view controller using the navigation stack, do the following:
NSArray* vcs = self.navigationController.viewControllers;
UIViewController* target = [vcs objectAtIndex:([vcs count] - 1) - numVCsToGoBack];
[self.navigationController popToViewController:target];
If you have to pop to specific view controller in the stack use following code:
(here consider you have to pop to MyViewController Class)
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[MyViewController class]])
{
[navigationController popToViewController:controller animated:YES];
break;
}
}

How to dismiss the PushViewController?

I have 10 viewControllers in my app. I have pushed all these viewControllers using pushViewController method of NavigationController on NEXT button clicked of each viewController.
I have used code for that as ;
[self.navigationController pushViewController:someView animated:YES];
Now i want to jump back to the rootViewController from the 10th child(viewController).
How can i do this ?
Please help me..
Thanks.
try to use ..
[self.navigationController popToRootViewControllerAnimated:YES];
Use this code:
[self.navigationController popToRootViewControllerAnimated:YES];
Try this
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:NO];//Use the desired index instead of 1 here
for just go back to last screen use this
[self.navigationController popViewControllerAnimated:YES]
NSInteger index = 0;
for (UIViewController *view in self.navigationController.viewControllers) {
if([view.nibName isEqualToString:#"RootViewController"])//put any `XIB name` where u want to navigate
break;
index = index + 1;
}
[[self navigationController] popToViewController:[[self.navigationController viewControllers] objectAtIndex:index] animated:YES];
To go back to the previous screen (last element of the view controller stack), pop the view controller:
[self.navigationController popViewControllerAnimated:YES];
you can use
[self.navigationController popViewControllerAnimated:YES]
for particular view ..
FOR SWIFT 4
self.navigationController?.popToRootViewController(animated: true)

How to navigate from one view controller to another view controller on button click?

I am new to iOS Application development, please help me how can I go from one view controller to another view controller on button click?
Follow the below step,let the button selector is
[button addTarget:select action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
and implement the selector as
-(void)buttonClick{
UIViewController *controler = [[UIViewController alloc] init];
[self.navigationController pushViewController:controler animated:YES];}
and also make sure viewController has NavigationController embedded within it and replace UIViewController with the Controller you wish to push.
Use this code in your Objective-C function for navigation -
DashboardViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"DashboardView"];
[dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:dvc animated:YES completion:nil];
Try this:
nextViewController *obj =[[nextViewController alloc]initWithNibName:#"nextViewController" bundle:nil];
[self.navigationController pushViewController:obj animated:YES];
[obj release];
You can use any of approach -
pushViewController: animated: - To Push the view on navigation stack
presentModalViewController:nc animated: - To present the view modally.
YourSecondViewcontroller *temp = [[YourSecondViewcontroller alloc]initWithNibName:#"YourSecondViewcontroller" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];
//or
[self presentModalViewController:temp animated:YES];
Visit this reference for tutorial and working demo code
Hope, this will help you..enjoy
//SAViewController will be your destiation view
// import SAViewController.h file in your current view
SAViewController *admin = [[SAViewController alloc]initWithNibName:#"SAViewController" bundle:nil];
[self presentModalViewController:admin animated:YES];
[admin release];
Try this code:
- (IBAction)btnJoin:(id)sender {
SecondViewController *ViewController2 = [self.storyboardinstantiateViewControllerWithIdentifier:#"SecondViewController"];
[self.navigationController pushViewController: ViewController2 animated:YES];
}

Resources