How to dismiss a collection of UIViewController ob

2019-09-12 17:25发布

问题:

I am having some trouble with dismissing a collection of UIViewController objects. I do not know of an elegant way to dismiss all loaded modal UIViewController objects at once.

I make use of the "storyboard" in Xcode 4, but I do not use Segues to map the screens. I use dynamic code, like so:

It all starts in MainViewController.m. I load [myMenuController] (a menu list). When the user selects an option from the menu, it then loads [myTicketController]. After some user interaction there, it loads [myNextController] and you see the pattern. I load several UIViewControllers dynamically (no segues here):

//from MainViewController.m - Load the main menu
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
myMenuController = (MyMenuController *)[storyboard instantiateViewControllerWithIdentifier:@"MyMenuController"];
myMenuController.settingsModel = settingsModel;
myMenuController.ticketStatusHandler = data;
myMenuController.rootDataModel = dataModel;
[myMenuController setModalPresentationStyle:UIModalPresentationFullScreen];    
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:myMenuController animated:YES];


///////////////////////////////////////////////////////////////////////////


//from MyMenuController.m - Load the add ticket menu
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
myTicketController = (MyTicketController *)[storyboard instantiateViewControllerWithIdentifier:@"MyTicketController"];
myTicketController.settingsModel = settingsModel;
myTicketController.ticketStatusHandler = data;
myTicketController.rootDataModel = dataModel;
[myTicketController setModalPresentationStyle:UIModalPresentationFullScreen];    
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:myTicketController animated:YES];


///////////////////////////////////////////////////////////////////////////////

//from MyTicketController.m - Load the next screen
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
myNextController = (MyNextController *)[storyboard instantiateViewControllerWithIdentifier:@"MyNextController"];
myNextController.settingsModel = settingsModel;
myNextController.rootDataModel = dataModel;
[myNextController setModalPresentationStyle:UIModalPresentationFullScreen];    
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController: myNextController animated:YES];

So I get to the end of the app and have loaded several more UIViewControllers. Let's say I load 4 or 5 more UIViewControllers after [myNextController] has been loaded.

Is there a generic way to unload all UIViewControllers that are in memory, and simply reload [myMenuController]?

回答1:

well you can define a global array in the app delegate for example and insert the view controllers inside it .. once you want to dismiss all view controllers make a for loop that dismiss all the objects inside it (the view controller ) ,, also you can insert a viewcontroller in specfic index if you want to load it for example

  1. To insert [appDelegate.controllersArr insertObject:self atIndex:0];

  2. To Unload all view Controllers

        for(int i=0;i<[appDelegate.controllersArr count];i++)
    {           
        [[appDelegate.controllersArr objectAtIndex:i] dismissModalViewControllerAnimated:NO];
    }
    

i hope this will be helpful .. good luck.



回答2:

If you want them to be “unloaded” (I assume you mean deallocated), you simply need to make sure there are no references to them. So you need to dismiss them (which you seem to already be doing), and you need to reset any variables that refer to them, e.g. in MyMenuController you need to do this when you're done with myTicketController:

[myTicketController release]; // if not using ARC
myTicketController = nil;  // if using ARC