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;
}
}
Related
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.
There are five controllers here, AViewController, BViewController, CViewController,DViewController,EViewController,controllers here,
A present---> B
B present---> C
C push--->D
D push--->E
Now, if I want to go back from EViewController to AViewController in one step, what code should I write?
[self.navigationController popToRootViewControllerAnimated:animated];
1) Getting the desirable ViewController as Below
for (id controller in [self.navigationController viewControllers])
{
if ([controller isKindOfClass:[AViewController class]])
{
[self.navigationController popToViewController:controller animated:YES];
break;
}
}
2) Here you have A,B,C,D,E Controllers. means A would be on 1 Position so what can you do
you can hard wired the Index as Below
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
3) Pop to the first viewController or rootViewController
[self.navigationController popToRootViewControllerAnimated:animated];
Use unwind segues.
In AViewController add bellow code
-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {
}
Go to User Interface of EViewController and Ctrl-drag from the button (you want set action) to the “Exit” outlet, you will see a modal popup.
You can do it recursively with generic solution. First of all you should have reference to A's navigation controller and then you should write recursive method to get active navigation controller like :`
-(UINavigationController*)getActiveNavigationController : (UINavigationController*)navigationController {
if ([navigationController.presentedViewController isKindOfClass:[AViewController class]]) {
return [self getActiveNavigationController:(UINavigationController*)((AViewController*)navigationController.presentedViewController)
];
}
if ((UINavigationController*)navigationController.presentedViewController == nil) {
return navigationController;
}
return [self getActiveNavigationController:(UINavigationController*)navigationController.presentedViewController];
}
`
After that you should write method like
-(void)getInitialScreen:(UINavigationController*)AViewControllerNavigationController {
if ([AViewControllerNavigationController.presentedViewController isKindOfClass:[AViewController class]]) {
return;
}
UINavigationController *navigation = [self getActiveNavigationController:AViewControllerNavigationController];
[navigation dismissViewControllerAnimated:YES completion:^{
[self getInitialScreen:AViewControllerNavigationController];
}];
}
finally after you wrote those 2 methods. You can call them like below and you can always get AViewController
[self getInitialScreen:AViewControlelrnavigationcontroller];
[AViewControlelrnavigationcontroller popToRootViewControllerAnimated:YES];
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;
}
}
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];
}
}
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];
}