【问题标题】:iOS App with multiple view controllers具有多个视图控制器的 iOS 应用
【发布时间】:2013-10-11 13:55:55
【问题描述】:

我在 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。

【问题讨论】:

  • 查看ios中的视图包含模式。 Viewcontrollers 可以有子 viewcontrollers 有他们自己的视图。这是一种非常简洁优雅的方式,可以将您的应用分解为可插入的小组件

标签: ios viewcontroller navigationcontroller


【解决方案1】:

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

祝你好运!

【讨论】:

  • 我是否必须使用将 NavigationController 添加到 AppDelegate 中的 self.window 而不是 self.Main?
  • self.Main = [[vcMain alloc] initWithNibName:@"vcMain" bundle:nil]; UINavigationController *nvController = [UInavigationController alloc]initwithRootViewController:self.Main]]; self.window.rootViewController = nvController; [self.window makeKeyAndVisible];
  • 是的,你必须以相同的开头,例如:self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; self.mainNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; self.window.rootViewController = self.mainNavigationController; [self.window makeKeyAndVisible];
  • 所以在 viewDidLoad 的主屏幕上我会这样做,如果我错了,请纠正我:vcDashboard *dash = [[vcDashboard alloc] initWithNibName:@"vcDashboard" bundle:nil] ; [self.navigationController pushViewController:dash 动画:NO]; ViewActive = vDashboard;
  • 是的,你是对的。如果您对此有任何问题,可以使用viewdidappear 方法而不是viewdidload 进行推送。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多