tags:

views:

385

answers:

2

My Secondview is BController *bview. now in this view there is 1 back button

on click of that back button -(IBAction)done:(id)sender { AController *aview= [[AController alloc] initWithNibName:@"AController" bundle:[NSBundle mainBundle]];

NSArray *array = [self.navigationController popToViewController: aview animated:YES];

}

AController *aview is nothing but my first view or u can say first view

but in click of back button i'm getting exception

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.' 2010-03-18 15:53:05.948 IChitMe[5072:207] Stack: ( 820145437, 837578260, 819694387, 814928571, 862794500, 862794216, 54911, 819902607, 861951876, 862404412, 819902607, 861951876, 861951724, 861951668, 861950732, 861953932, 861948160, 861945748, 861927312, 861925524, 858687888, 819893547, 819891231, 858682228, 861592624, 861585968, 10069, 9964 ) terminate called after throwing an instance of 'NSException'

+1  A: 

Use

[self.navigationController popViewControllerAnimated:YES];

OR

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
mihirpmehta
+2  A: 

navigationController manages stack of UIViewControllers. It's like stack of cards. When you call one of pop methods:

  • (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
  • (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
  • (UIViewController *)popViewControllerAnimated:(BOOL)animated

you remove some controllers from top, and show corresponding controller:

  • (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
    pops to first (i.e. lowest in stack) controller, it is called "root".

  • (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
    pops to specified viewController, and note it should be already in the stack!

  • (UIViewController *)popViewControllerAnimated:(BOOL)animated
    pops to previous controller (below current).

So if you want to show your AController *aview using pop... method of navigationController it should be already in navigationController stack - this is viewControllers property of navigationController:

@property(nonatomic, copy) NSArray *viewControllers

for example:

aController = [[AController alloc] initWithNibName:@"AController" bundle:[NSBundle mainBundle]];
bController = [[BController alloc] initWithNibName:@"BController" bundle:[NSBundle mainBundle]];
navigationController = [[UINavigationController alloc] initWithRootViewController:aController]; 
[navigationController pushViewController:bController];

now bController is shown and you can call:

[navigationController popToViewController:aController animated:YES];
Vladimir
how to take outlet ,property and synthesize of viewControllers
I used as u tell but i'm getting blank whiteview
what is this NSArray *viewControllers?
When I try `[navigationController popToViewController:aController animated:YES];` it said that `aController is undefined`. It make sense since I am in `bController` now, I dont have a reference of `aController`, right? or Am I missing something here?
Harry Pham
Yes, if you want to pop to definite controller you should have a reference to it. Most common practice to have all necessary references in object accessable from all controllers, for example it can be your application delegate. If you define in MyAppDelegate.h property @property (retain) UIViewController * bController;You can access appdelegete anywhere as MyAppDelegate * appDelegate =(MyAppDelegate *)[[UIApplication sharedApplication] delegate]; And then you can use appDelegate.bController;
Vladimir