hacker hacker - 4 months ago 10
iOS Question

How to get a particular view controller from the stack of viewcontrollers in the navigation controller?

I have an application in which i am having a table view.when pressing a button on the cell i have to go to another view controller(ie:testviewcontroller) showing what i selected in the previous one.then when pressing one button i need to go to another view controller showing the remaing values.if he select one there then i need to repeat the above process.The problem is from here also i am carrying some values to that testviewcontroller.if i pop back to my view controller how can i carry the new values.currently i am doing like this.

TestViewController *test =[[ TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];


test.itemselected=head;

test.itemid=productid;
//NSLog(@"%@",del.navigationController);

[del.navigationController pushViewController:test animated:YES];


but i know

NSArray *array = [del.navigationController viewControllers];


[array objectAtIndex:3]
is my desired view controller.

can anybody know how can i avoid this pushing of same view controller again?

Answer
for (UIViewController*vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass: [TestViewController class]]){
vc.itemselected= head ; 
[[self navigationController] popToViewController:vc animated:YES];
  }
}

*EDIT* This should be

for (TestViewController*vc in [self.navigationController viewControllers])

instead of

for (UIViewController*vc in [self.navigationController viewControllers])
Comments