2

對於某些人來說,這可能看起來像一個愚蠢的問題,但在iPhone應用程序中處理不同類型的控制器對我來說仍然有點模糊。這裏的設置:將導航控制器添加到選項卡欄應用程序(以編程方式)

我有一個選項卡欄應用程序與四個選項卡。每個選項卡都將控制權交給其各自的ViewController,其中一些選項用.XIB文件初始化,一些則完全以編程方式完成。其中一個程序化的是DirectionsViewController,其本質上是UITableViewController。從表格中選擇一個單元格需要(模態地)呈現DetailedDirectionsViewController,它需要對呈現視圖控制器進行某種反向引用。我認爲最簡單的方法是將導航控制器添加到DirectionsDetailedDirections VC - 除非我不知道如何在沒有.XIB文件的情況下執行此操作。

而且,我用手控制權交給DetailedDirections的方法是通過改變Directions方式如下:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    [self.tabBarController presentModalViewController:vc animated:YES]; 
} 

我似乎記得我的教授說,presentModalViewController是一種古老的方法之一,並有更好的替代品...我現在不記得他們。

+0

看看http://bit.ly/H4kHEZ,可以看看該文檔中的示例代碼。 – 2012-03-30 18:39:51

回答

1

對於你想要做的事情,最好是讓你的tabbar中的標籤管理一個UINavigationController,並將該導航控制器的rootViewController設置爲你的DirectionsViewController。

然後在你的方向視圖控制器的didSelectRowAtIndexPath:方法,你可以做到以下幾點:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailedDirectionsViewController *vc = [[DetailedDirectionsViewController alloc] initWithStyle:UITableViewStyleGrouped]; 
    [self.navigationController pushViewController:vc animated:YES]; 
} 

它將會像你想讓它。 UINavigation控制器將負責在您的詳細路線視圖控制器上放置一個後退按鈕。

+0

謝謝,我當時很傻。 – Argent 2012-04-10 18:09:33

0

如果我理解的很好,在tableview:didSelectRow ..:你只需要創建一個導航控制器用你想要模態顯示的視圖控制器初始化它,然後呈現它創建一個UIBarButtonItem並將其添加到導航作爲選擇器的導航控制器欄創建一個新的方法與內部dismiss命令。

相關問題