带有多个视图控制器的iOS App

时间:2013-10-11 13:55:56

标签: ios viewcontroller navigationcontroller

我在appDelegate上的当前应用程序加载一个main.xib屏幕,该屏幕仅包含两个图像背景和徽标。此代码后面的屏幕确定用户是否已登录系统,否则它将显示登录,否则它将显示仪表板。

该应用程序已创建为单个视图应用程序,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];
}

在Main.m上,我在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];
}

仪表板上有一个菜单按钮,向用户显示一系列选项以选择另一个屏幕,按下该按钮将激活以下方法:

- (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;
}

}

我的疑问是,我似乎不知道当从这个菜单中选择一个选项时这是否是在屏幕之间切换的正确方法,并且如果总是将子视图添加到主屏幕是正确的。不知道使用navigationcontroller模板是否是一种解决方案。我担心应用程序在执行所有操作时所消耗的内存,我目前正在项目中使用ARC。

1 个答案:

答案 0 :(得分:0)

我建议您尽可能避免使用addSubview方法。 UiNAvigationController为您提供了处理不同viewControllers的好方法。例如,如果您创建addRubview,则不会调用changeRotation事件。当你进行弹出时,viewController被释放。

祝你好运!