Ios UINavigationController初始化WithRootViewController,视图未出现

Ios UINavigationController初始化WithRootViewController,视图未出现,ios,ipad,uinavigationcontroller,uitableview,Ios,Ipad,Uinavigationcontroller,Uitableview,我正试图获得一个基于导航的功能,以显示不同的表视图,但没有运气。实际上,用于initWithRootViewController的视图没有正确显示,但导航栏显示不正确。以下是具有层次结构的AppDelegate->ViewController的viewdiload方法中的代码TimerViewController: incidentTableViewController = [[IncidentTableViewController alloc] initWithStyle:UITableVie

我正试图获得一个基于导航的功能,以显示不同的表视图,但没有运气。实际上,用于
initWithRootViewController
的视图没有正确显示,但导航栏显示不正确。以下是具有层次结构的
AppDelegate
->
ViewController
viewdiload
方法中的代码
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]我得到的结果符合要求,减去导航栏:


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

为什么要这样做

[controller.navigationController setNavigationBarHidden:YES];
顺便说一句,在AppDidFinish启动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,那么您应该将incidentTableViewController推送到nav堆栈上,或者以模式显示incidentTableViewController。您必须更改项目的逻辑。您可以启动一个新的选项卡项目并修改didFinishLaunchingWithOptions方法 在这里,我用两个选项卡显示示例,一个选项卡带有示例视图,另一个选项卡带有工具栏:

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

UINavigationController没有自定义视图

 [self.view addSubview:controller.view];
--->


使用以导航控制器作为其视图控制器初始化的
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];

在appDelegate中,您使用哪种方法编写代码?还是要在单击时显示带有导航的视图?代码是在另一个类中编写的,该类具有
TimerViewController
viewdiload
方法中的层次结构
AppDelegate
->
ViewController
。您希望实现什么?带条的viewcontroller,如何命名?当您单击按钮时?我希望第二个图像(正确显示的表格视图)显示为导航栏此工具栏视图是您的初始视图(当应用程序启动时,您首先看到它)?抱歉。不是[self.view…]-->[self…]很抱歉,我应该说得更清楚一些,我不希望在应用程序启动时显示此特定视图。在这种情况下,您需要执行相同的操作,而不将其添加到窗口中,而是将视图控制器推到导航堆栈上或以模式显示视图控制器。如果您仅为工具栏使用导航控制器,则可以使用UIToolbar。谢谢,但更改没有任何效果
[self presentViewController:controller animated:YES completion:^{}];
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];