问题
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