views:

260

answers:

1

I have a UINavigationController . I have to pop a view from a UINavigationController and replace it with another. How we can search for a UIviewcontroller object and replace it with another ?

when i print

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: myDelegate.navigationController.viewControllers];

I tried

[allViewControllers removeObjectIdenticalTo: @"NonLogginedViewController"];
[allViewControllers removeObjectIdenticalTo: myDelegate.nonLogginedViewController]; myDelegate.navigationController.viewControllers = allViewControllers;

But it didn't update the uinavigationcontroller stack .. I don't know how to use removeObjectIdenticalTo with uinavigationcontroller stack..

Please help me ..

+3  A: 


Firstly, your test:

[allViewControllers removeObjectIdenticalTo: @"NonLogginedViewController"];

...is testing for a string, not a view controller. So that won't work.

If you know where the view controller is in the navigation controller's stack then this is easy. Say for example you've just pushed a new controller and now you want to remove the one before that. You could do this:

NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];

[allControllers removeObjectAtIndex:[allControllers count] - 2];
[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];

But I think in your case you want to find a certain controller and remove it. One way to do this would be to look for a certain class, e.g. LoginController. Set up your array as above and then:

for (id object in allControllers) {
  if ([object isKindOfClass:[LoginController class]])
     [allControllers removeObject:object];
}

then set the viewControllers as before.

imnk
Thank you. You saved my time..Thanks for giving descriptive answer. It helped me to learn how to iterate NSMutablearry..
Sijo