简体   繁体   中英

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?

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])

For your condition it is beter to pop back and reload the table with new contents..no need to push to a new instance

Use a seperate array for datasource of table

remove the selected item from datasource array

then push to next view when poped back reload contents

Happy coding :)

You can traverse loop on view controller stack.

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {

        NSMutableArray *ViewControllerArray=[[NSMutableArray alloc]init];
        for (long i=[self.navigationController.viewControllers count]-1;i>0; i--) {


            [ViewControllerArray addObject:[self.navigationController.viewControllers objectAtIndex:i]];

            NSLog(@"%@",ViewControllerArray);
            NSLog(@"%@",self.navigationController.viewControllers);
        }

        for (UIViewController *controller in self.navigationController.viewControllers) {

            //Do not forget to import AnOldViewController.h
            if ([controller isKindOfClass:[YourViewController class]]) {

                [self.navigationController popToViewController:controller
                                                      animated:YES];
                break;
            }
        }



    }
    [super viewWillDisappear:animated];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM