2012-07-31 96 views
0

我试图让一个基于导航的功能显示不同的表视图,没有运气。本质上用于initWithRootViewController的视图没有正确显示,但导航栏是。下面是与层次TimerViewControllerviewDidLoad方法中的代码AppDelegate - >ViewController - >TimerViewControllerUINavigationController initWithRootViewController,视图不出现

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]; 

这个结果与(我不知道为什么会出现导航栏上方的缝两边):

enter image description here

如果我取消从最后一行[controller.view addSubview:incidentTableViewController.view];第二我得到这样的结果是因为需要减去导航栏:

enter image description here

我想要实现的是第二个图像与导航栏,任何想法?

+0

用哪种方法在appDelegate中编写代码?或者你想通过点击导航来显示你的视图? – Alex 2012-07-31 14:43:32

+0

该代码是在'TimerViewController'的'viewDidLoad'方法内的另一个具有'AppDelegate' - >'ViewController' - >'TimerViewController'类的类中编写的。 – 2012-07-31 14:52:11

+0

你想要达到什么目的?带有栏的视图控制器,它是如何调用的?当你点击一个按钮? – Alex 2012-07-31 14:55:25

回答

0

修正通过使用与导航控制器作为其视图控制器初始化一个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]; 

enter image description here

0

你为什么要这样做?

[controller.navigationController setNavigationBarHidden:YES]; 

顺便说一句,在你对你的appDelegate的appdidfinishlaunching做到这一点:

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到导航堆栈或呈现incidentTableViewController模态

+0

对不起,我应该已经更清楚了,在这种情况下,当应用程序启动 – 2012-07-31 15:04:02

+0

时,您希望这个特定的视图出现,但您需要做同样的事情,而不必将其添加到窗口中,而是将视图控制器推入导航堆栈或通过以模态方式呈现视图控制器。如果您只是为该栏使用导航控制器,则可以改为使用UIToolbar。 – 2012-07-31 17:16:31

0

你必须改变你的项目的逻辑。你可以开始一个新的标签项目和修改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; 
} 
+0

谢谢,但更改没有任何影响 – 2012-07-31 15:17:43

+0

我修改了答案 – Alex 2012-07-31 15:31:08

0

的UINavigationController没有你的自定义视图。

[self.view addSubview:controller.view]; 

--->

[self presentModalViewController:controller animated:YES]; 

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

谢谢,已经尝试过,但没有出现 – 2012-07-31 14:58:23

+0

对不起。不[self.view ...] - > [self ...] – booiljoung 2012-07-31 15:03:29

+0

还没有:( – 2012-07-31 15:10:55

相关问题