2012-07-31 95 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

相關問題