How to iterate all UIViewControllers in an application

I have xib with this structure:

- Tab Bar Controller -- Tab Bar -- NavigationController --- Navigation Bar --- News List View Controller ---- Navigation Item --- Tab Bar Item -- NavigationController --- Navigation Bar --- News List View Controller ---- Navigation Item --- Tab Bar Item -- NavigationController --- Navigation Bar --- News List View Controller ---- Navigation Item --- Tab Bar Item [...] 

How can I code a loop for each UIViewController (News List View Controller) at each iteration?

+8
ios objective-c iphone
source share
2 answers

Access to them like this:

 NSArray * controllerArray = [[self navigationController] viewControllers]; for (UIViewController *controller in controllerArray){ //Code here.. eg print their titles to see the array setup; NSLog(@"%@",controller.title); } 
+8
source share

If you are using iOS 5, you can do something like this:

 - (void) processViewController: (UIViewController *) viewController { //do something with viewcontroller here NSLog(@"I'm viewcontroller %@", viewController); for ( UIViewController *childVC in viewController.childViewControllers ) { [self processViewController:childVC]; } } 

and start all the fun:

 [self processViewController:myRootViewController]; //would be the tabbarcontroller in your case 

Edit: I'm not sure what you want to achieve here, but this code is for traversing an entire tree.

Edit 2:

For iOS 4, try something like this:

 - (void) processViewController: (UIViewController *) viewController { //do something with viewcontroller here NSLog(@"I'm viewcontroller %@", viewController); if ( [viewController isKindOfClass:[UITabBarController class]] ) { for ( UIViewController *childVC in ((UITabBarController *)viewController).viewControllers ) { [self processViewController:childVC]; } } else if ( [viewController isKindOfClass:[UINavigationController class]] ) { for ( UIViewController *childVC in ((UINavigationController *)viewController).viewControllers ) { [self processViewController:childVC]; } } } 

Note. You will need to add any custom view manager that has subviewcontrollers. If you have any ... The root view controller gets it again.

+4
source share

All Articles