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

怎甘沉沦 提交于 2021-02-05 05:54:25

问题


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?


回答1:


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



回答2:


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




回答3:


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];
}


来源:https://stackoverflow.com/questions/11966012/how-to-get-a-particular-view-controller-from-the-stack-of-viewcontrollers-in-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!