MMDrawerController and instantiating many view controllers

ios, objective-c, viewcontroller

Solution

This method is working well for me and there is no need to instantiate a new view controller or navigation controller every time the user changes views.

Declare a mutable array to hold your navigation controllers:

@property (nonatomic, strong) NSMutableArray *navigationControllerArray;

Initialise and fill the array with the same number of empty strings as you have view controllers:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationControllerArray = [[NSMutableArray alloc] initWithObjects:@"",@"",@"",nil];

}

In your UITableViewDelegate, check to see if the object at the selected row is a UINavigationController. If it is not, instantiate a new navigation controller and replace the empty string in your navigation controller array with it.

Set this navigation controller as the center view controller:

#pragma mark - UITableViewDelegate

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSObject *navigationController = [self.viewControllerArray objectAtIndex:indexPath.row];

    if (![navigationController isKindOfClass:[UINavigationController class]]) {

        UIViewController *newViewController;

        switch (indexPath.row) {
            case 0:
                newViewController = (UIViewController *)[[AccountListTableViewController alloc] init];
                break;
            case 1:
                newViewController = (UIViewController *)[[PageDetailViewController alloc] init];
                break;
            case 2:
                newViewController = (UIViewController *)[[LoginViewController alloc] init];
                break;

            default:
                newViewController = (UIViewController *)[[AccountListTableViewController alloc] init];
                break;
        }

        navigationController = (UINavigationController *)[[UINavigationController alloc] initWithRootViewController:(UIViewController *)newViewController];

        [self.viewControllerArray replaceObjectAtIndex:indexPath.row withObject:navigationController];

    }

    [self.mm_drawerController setCenterViewController:(UINavigationController *)navigationController withCloseAnimation:YES completion:nil];

    return indexPath;
}

Problem

This is a specific MMDrawerController question, although probably relates to other iOS drawer controllers out there. I've got my MMDrawerController working nicely, and can load different view controllers into the 'center' by selecting a row in my drawer tableview. However, I want to avoid instantiating my view controllers every time I select a menu item in my drawer. It seems inefficient, especially if the user will switch between screens many times during a session. I'm guessing a better way is to store the (instantiated) view controllers I'm using as a variable and to reuse? I have quite a few view controllers to potentially load into the center, each of which will do some 'work' initially in viewDidLoad. Here is how I am loading a new view controller into the center (in my DrawerViewController.didSelectRowAtIndexPath method): ``` MyViewConroller * newCenter = [[MyViewConroller alloc] init]; UINavigationController * nav = [[MMNavigationController alloc] initWithRootViewController:newCenter]; if(indexPath.row==0){ [self.mm_drawerController setCenterViewController:nav withCloseAnimation:YES completion:nil]; } ``` Thoughts/comments about the best approach of loading these view controllers would be appreciated.

Original source