0

My current application on appDelegate loads a main.xib screen which only contains an two images background and logo. This screen behind code determines if the user is logged on the system if not it will show the login else it will show the dashboard.

The application has been created as a single view application, sample code of the appDelegate:

  // Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
{
    self.Main = [[vcMain alloc] initWithNibName:@"vcMain" bundle:nil];
    self.window.rootViewController = self.Main;
    [self.window makeKeyAndVisible];
}
else
{
    self.MainiPad = [[vcMain_iPad alloc] initWithNibName:@"vcMain_iPad" bundle:nil];
    self.window.rootViewController = self.MainiPad;
    [self.window makeKeyAndVisible];
}

On the Main.m I have the following on the viewDidLoad:

if (islogged)
{
     vcDashboard *vcDash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil];
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcDash];
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque;
    _ncMain.view.frame = self.view.bounds;
    [self.view addSubview:_ncMain.view];
    ViewActive = vDash;
}
else
{
    vcLogin *login = [[vcLogin alloc] initWithNibName:@"vcLogin" bundle:nil];
    login.modalPresentationStyle = UIModalPresentationFormSheet;
    login.view.frame = self.view.bounds;
    [self presentViewController:login animated:YES completion:nil];
}

There is a menu button available on the Dashboard that presents the user with a series of options to select another screen and when pressed it will activate the following method:

- (void)displayView:(NSString *)strView Notification:(NSNotification *)notification{

if(_ncMain)
{
    [_ncMain.view removeFromSuperview];
    _ncMain = nil;
}

if ([strView isEqual: @"Dashboard"])
{
    vcDashboard *vcDash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil];
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcDash];
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque;
    _ncMain.view.frame = self.view.bounds;
    [self.view addSubview:_ncMain.view];
    ViewActive = vDash;
}
else if ([strView isEqual: @"Catalog"])
{
    vcCatalog *vcCat = [[vcCatalog alloc] initWithNibName:@"vcCatalog" bundle:nil];
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcCat];
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque;
    _ncMain.view.frame = self.view.bounds;
    [self.view addSubview:_ncMain.view];
    ViewActive = vCatalog;
}
else if ([strView isEqual: @"News"])
{
    vcNews *vcNew = [[vcNews alloc] initWithNibName:@"vcNews" bundle:nil];
    _ncMain = [[UINavigationController alloc] initWithRootViewController:vcNew];
    _ncMain.navigationBar.barStyle = UIBarStyleBlackOpaque;
    _ncMain.view.frame = self.view.bounds;
    [self.view addSubview:_ncMain.view];
    ViewActive = vNews;
}

}

My doubt here is I don't seem to know if this is the correct way of changing between screens when an option is selected from this menu and if is right to always addSubview to the principal screen. Don't know if using the navigationcontroller template is a solution. I'm concern of the memory consumed by the app when doing all of this, also I'm currently using ARC on the project.

Angie
  • 475
  • 2
  • 6
  • 20
  • Look at the view containment pattern in ios. Viewcontrollers can have sub viewcontrollers with their own views. Its a very neat and elegant way of breaking your app up into small pluggable components – Zayin Krige Oct 11 '13 at 14:16

1 Answers1

0

I recommend you that avoid the addSubview method if possible. UiNAvigationController offer you a good way to handle different viewControllers. If you make addSubview the changeRotation event, for example, is not called. And when you make a pop the viewController is dealloced.

Good luck!

Andrés Brun
  • 185
  • 9
  • Do I have to use add the NavigationController to the self.window in the AppDelegate instead of the self.Main? – Angie Oct 11 '13 at 14:15
  • self.Main = [[vcMain alloc] initWithNibName:@"vcMain" bundle:nil]; UINavigationController *nvController = [UInavigationController alloc]initwithRootViewController:self.Main]]; self.window.rootViewController = nvController; [self.window makeKeyAndVisible]; – Angie Oct 11 '13 at 14:17
  • 1
    Yes, yo have to start with samething like: `self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; self.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; self.window.rootViewController = self.mainNavigationController; [self.window makeKeyAndVisible];` – Andrés Brun Oct 11 '13 at 14:18
  • So on the Main screen in the viewDidLoad I would do some like this, correct me if I'm wrong: vcDashboard *dash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil]; [self.navigationController pushViewController:dash animated:NO]; ViewActive = vDashboard; – Angie Oct 11 '13 at 14:27
  • Yes, you are right. If you have any problem with that you can do the push in `viewdidappear` method instead `viewdidload`. – Andrés Brun Oct 11 '13 at 15:36