【问题标题】:UINavigationController initWithRootViewController, view not appearingUINavigationController initWithRootViewController,视图没有出现
【发布时间】:2012-07-31 14:32:47
【问题描述】:

我正在尝试获得基于导航的功能来显示不同的表格视图,但没有成功。本质上,用于initWithRootViewController 的视图没有正确显示,但导航栏是。这是TimerViewControllerviewDidLoad 方法中的代码,其层次结构为AppDelegate -> ViewController -> TimerViewController

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    [incidentTableViewController.view setFrame:CGRectMake(0, 0, 268, 423)];
    [incidentTableViewController.tableView showsVerticalScrollIndicator];
    [incidentTableViewController setTitle:@"Incidents"];
    [incidentTableViewController.navigationController setNavigationBarHidden:NO];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
    [controller.view setFrame:CGRectMake(268, 0, 268, 423)];
    [controller.view setBackgroundColor:[UIColor clearColor]];
    [controller.navigationController setNavigationBarHidden:YES];
    //[controller.view addSubview:incidentTableViewController.view];
    [self.view addSubview:controller.view];

这会导致(我也不确定导航栏上方为什么会有空隙):

如果我取消注释倒数第二行 [controller.view addSubview:incidentTableViewController.view]; 我会得到这个结果,这是减去导航栏所需的结果:

我想要实现的是第二张带有导航栏的图片,有什么想法吗?

【问题讨论】:

  • 您在 appDelegate 中使用哪种方法编写代码?还是您想通过点击显示您的视图?
  • 代码写在另一个类中,层次结构为AppDelegate -> ViewController -> TimerViewControllerviewDidLoadviewDidLoad 方法中TimerViewController
  • 你想要实现什么?带bar的viewcontroller,怎么叫?当你点击一个按钮?
  • 我希望用导航栏显示第二张图片(表格视图显示正确)
  • 这个带有工具栏的视图是你的初始视图(当应用启动时你首先看到它)?

标签: ios ipad uinavigationcontroller uitableview


【解决方案1】:

你为什么要这样做?

[controller.navigationController setNavigationBarHidden:YES];

顺便说一句,在你的 appdidfinishlaunching 你的 appDelegate 中这样做:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

[incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
[incidentTableViewController.view setFrame:CGRectMake(0, 0, 268, 423)];
[incidentTableViewController.tableView showsVerticalScrollIndicator];
[incidentTableViewController setTitle:@"Incidents"];
[incidentTableViewController.navigationController setNavigationBarHidden:NO];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
[controller.view setFrame:CGRectMake(268, 0, 268, 423)];
[controller.view setBackgroundColor:[UIColor clearColor]];
self.window.rootViewController = incidentTableViewController;

[self.window makeKeyAndVisible];

如果您的窗口有一个 rootViewcontroller,那么您应该将事件表视图控制器推送到导航堆栈或以模态方式显示事件表视图控制器

【讨论】:

  • 抱歉,我应该更清楚一点,我不希望在应用程序启动时出现此特定视图
  • 在这种情况下,您需要执行相同的操作,但不将其添加到窗口中,而是将视图控制器推送到导航堆栈或以模态方式呈现视图控制器。如果您只为栏使用导航控制器,则可以改用 UIToolbar。
【解决方案2】:

您必须更改项目的逻辑。可以新建一个tab项目,修改didFinishLaunchingWithOptions方法 这里我展示了带有 2 个选项卡的示例,一个带有示例视图的选项卡,另一个带有工具栏的选项卡:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //view controller for 1st tab
    UIViewController * viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil];

    //your view controller with bar for the 2d tab
    IncidentTableViewController *incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    [incidentTableViewController.tableView.backgroundView setBackgroundColor:[UIColor colorWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    [incidentTableViewController.tableView showsVerticalScrollIndicator];
    [incidentTableViewController setTitle:@"Incidents"];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, controller, nil];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

【讨论】:

  • 谢谢,但更改没有任何效果
【解决方案3】:

通过使用UISplitViewController初始化导航控制器作为其视图控制器修复问题,工作代码如下:

incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:incidentTableViewController];
    incidentTableViewController.title = @"Incidents";
    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects:controller, nil];
    splitViewController.delegate = (id)incidentTableViewController;
    splitViewController.view.frame = CGRectMake(268, 0, 268, 423);
    [self.view addSubview:splitViewController.view];

【讨论】:

    【解决方案4】:

    UINavigationController 没有您的自定义视图。

     [self.view addSubview:controller.view];
    

    --->

     [self presentModalViewController:controller animated:YES];
    

    [self presentViewController:controller animated:YES completion:^{}];
    

    【讨论】:

    • 对不起。不是 [self.view ...] --> [self ...]
    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多