0

我当前在 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];
}

Dashboard 上有一个可用的菜单按钮,它为用户提供一系列选项来选择另一个屏幕,按下它会激活以下方法:

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

}

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

4

1 回答 1

0

如果可能,我建议您避免使用 addSubview 方法。UiNAvigationController 为您提供了一种处理不同视图控制器的好方法。例如,如果您制作 addSubview,则不会调用 changeRotation 事件。当你弹出时, viewController 被释放。

祝你好运!

于 2013-10-11T14:09:11.817 回答