2010-07-20 82 views
0

我想問一個關於iPhone應用程序的問題。我寫了一個會顯示一個表的程序。但是,我不知道爲什麼我無法在表格中顯示導航標題。下面是我的代碼無法在iPhone應用程序的表格中顯示導航標題

//代碼

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    // control flow, call another user define function and add the items to the table 
    [self flowControl]; 

    //Set the title  
    self.navigationItem.title = @"Table Title Hello"; 

    // can see 
    // NSLog(@"self.navigationItem.title: %@", self.navigationItem.title); 

} 

表只能顯示項,但不是標題。誰能幫我?

// ----------更新1 --------------

在[自流量控制]的代碼將調用兩個功能,這兩者的功能是添加項目,例如[displayNameArray addObject:@"John Chan"];

// ---------- Update 2 --------------- 在我的程序中,是2個視圖控制器。

1)MyViewController

2)SimpleTableView

第一個是用來讓用戶輸入的信息,第二個是用於顯示在表格式的內容。

我的項目名稱是USERPROJECT

//The following is the content of the .m 
#import "MyViewController.h" 
#import "USERPROJECT.h" 
#import "SimpleTableView.h" 

@implementation USERPROJECT 

@synthesize window; 
@synthesize myViewController; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]]; 
    [self setMyViewController:aViewController]; 
    [aViewController release]; 

    UIView *controllersView = [myViewController view]; 
    [window addSubview:controllersView]; 

    // Override point for customization after application launch 
    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [myViewController release]; 
    [window release]; 
    [super dealloc]; 
} 

@end 

而且它是「MyViewController.m」,我用它來從「MyViewController.m」切換到控制「SimpleTableView中的內容之一。 m'

- (void) switchPageShowTable { 

    NSLog(@"%d: switchPageShowTable", order); 
    order++; 

    SimpleTableView *simpleTableView = [[SimpleTableView alloc] initWithNibName:nil bundle:nil]; 
    [self presentModalViewController:simpleTableView animated:YES]; 

} 

它是否會影響在調用標題中使用'self'?非常感謝你。

回答

2

您期望看到的標題顯示在導航欄中,該導航欄通常是UINavigationController的一部分。爲了使它工作,你必須做到以下幾點:

SimpleTableView *simpleTableView = [[SimpleTableView alloc] initWithNibName:nil bundle:nil]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: simpleTableView]; 
[self presentModalViewController:navController animated:YES]; 
[simpleTableView release]; 
[navController release]; 

而不是

self.navigationItem.title = @"Table Title Hello"; 

(小雞蛋裏挑骨頭),你可以這樣做:除非你有充分的理由

self.title = @"Table Title Hello"; 

明確使用navigationItem。

+0

感謝您的回覆。我嘗試將代碼放在SimpelTableView.h中,但收到很多錯誤。我應該在哪裏放置代碼? – Questions 2010-07-21 01:16:34

+0

第一塊代碼替換了你在switchPageShowTable中的代碼(我認爲它是MyViewController的方法)。請注意,我的版本釋放模態視圖控制器,以便您不必在別處跟蹤它。 「self.title」部分是SimpleTableView的職責,可能會進入init ...或viewDidLoad。順便說一下,SimpleTableView的名稱有點混亂,我將其命名爲SimpleTableViewController類。 – Costique 2010-07-21 04:58:56

0

是您試圖加載UITableViewController的子類的視圖?

那麼[self flowControl]究竟做了什麼?

+0

感謝您的回覆。我已經在上面編輯了這篇文章,你會看看。此外,你是什麼意思「是你試圖加載UITableViewController的子類的視圖?」。我是目標C的綠色,你想介意給我解釋一下嗎? – Questions 2010-07-20 03:26:03

+0

界面看起來像這樣嗎? @interface ViewController:UITableViewController {} @end – 2010-07-20 03:31:11

+0

感謝您的回覆,並且我更新了上面的帖子,您是否介意查看。我在上面添加了我的項目結構。此外,界面代碼就像你輸入的內容。謝謝。 – Questions 2010-07-20 03:49:42

相關問題