2012-04-26 41 views
1

我是Objective-C中的新成員,並且我將開始研究具有幾個View的應用程序。我想以編程方式100%地創建它們,而不需要XIB文件。我知道我必須爲我的屏幕創建ViewControllers類,但我想問您如何使用它管理導航。假設我有一個ViewController,其中包含tableView。我想創建下一個屏幕。所以,據我所知,在rowtableViewController調用以編程方式創建新視圖並管理它們

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //... } 

但對於下一個畫面?我還沒有。我應該創建一個「準備好」ViewController類嗎?在這種情況下如何處理導航?

回答

3

在你的情況下,當選擇一個小區,你會:

  • 創建你的下一個視圖控制器的新實例,
  • 推這個新實例的UINavigationController堆棧。

因此,首先,您需要確保您的第一個視圖控制器(具有表視圖的控制器)包含在UINavigationController中。

// AppDelegate, in applicationDidFinishLanching: 

UIViewController *firstViewController = [[[MyCustomTableViewController alloc] 
              initWithNibName:nil bundle:nil] 
             autorelease]; 
UINavigationController *navigationController = [[[UINavigationController alloc] 
               initWithRootViewController:firstViewController] 
               autorelease]; 

[self.window setRootViewController:navigationController]; 

然後,當選擇你的表視圖中的單元格,你可以寫:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIViewController *nextViewController = [[[MyNextViewController alloc] 
              initWithNibName:nil bundle:nil] 
              autorelease]; 

    [self.navigationController pushViewController:nextViewController 
             animated:YES] 

} 
+0

謝謝,這是有益的。 – Kuba 2012-04-26 08:54:17

+0

還有一個問題。我必須製作一個多重的ViewControllers或者「重新加載」一個我已經擁有的?或者也許創建這個類的新實例? – Kuba 2012-04-26 09:00:19

+1

這真的是你開始需要的開始和很好的解釋。還有一件事:UINavigationController的類參考包含一個很好的介紹:http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference。html – 2012-04-26 09:01:11

2

請務必閱讀來自蘋果的文檔:View Controllers Programming Guide

基本上,你將不得不在導航控制器(容器視圖控制器)中嵌入您的表格視圖控制器(作爲視圖控制器的內容)。

然後在-tableView:didSelectRowAtIndexPath方法:,你將實例化一個新內容視圖控制器,並用這種消息的推動它當前表視圖控制器上,通過導航控制器:

[[self navigationController] pushViewController:<#myNextViewController#> animated:YES]; 

請務必閱讀來自Apple的代碼示例(有些非常簡單,所以很容易理解)。

相關問題