2011-10-02 88 views
0

我有以下幾點:navigationController問題

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

    audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];  

    [self.navigationController pushViewController:audiChassisInputViewController animated:YES]; 

    self.navigationController.navigationBarHidden = NO; 

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)]; 
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:retourButton]; 
    [self.navigationController.navigationBar.topItem setTitle:@"Chassis Input"]; 
    [retourButton release]; 

    [audiChassisInputViewController release]; 

} 

這workes ...新的視圖顯示。

在新視圖

myAudiChassisInputViewController.h

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    chassisInputTextView.layer.cornerRadius = 15; 
    chassisInputTextView.clipsToBounds = YES; 
    [chassisInputTextView becomeFirstResponder]; 

    UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)]; 
    [self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton]; 
    [okButton release]; 

} 

我沒有錯誤,但並無大礙欄按鈕shown.Anyone,任何想法,爲什麼?

回答

2

改變這一行:

[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton]; 

與此行:

[[self navigationItem] setRightBarButtonItem:okButton]; 

關鍵是,通過執行viewDidLoad時,導航欄(self.navigationController.navigationBar.topItem)頂部的項目仍然指向後視圖控制器的導航項目。

後視圖控制器是當前頂視圖控制器被推入堆棧之前曾經是頂視圖控制器的那個([[viewControllers objectAtIndex:[viewControllers count] - 2] navigationItem])。下面的代碼片段顯示瞭如何將導航欄的頂部項目仍然指向在viewDidLoad後視圖控制器的導航項目,它是僅用於說明目的:

// the view controllers currently on the navigation stack 
NSArray *viewControllers = self.navigationController.viewControllers; 
// The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array. 
UIViewController *backViewController = [viewControllers objectAtIndex:[viewControllers count] - 2]; 
// get the navigation item of the back view controller 
UINavigationItem *backNavigationItem = backViewController.navigationItem; 
UINavigationItem *topItem = self.navigationController.navigationBar.topItem; 
if (backNavigationItem == topItem) { 
    NSLog(@"This gets logged to the console"); 
} 
+0

它的工作原理...但我仍然不明白你的解釋...你能爲我編輯你的答案嗎?謝謝 – adrian

+0

@george我編輯了答案。我希望現在更清楚。 – albertamg

+0

這次很好的解釋! – adrian

1

轉到您的

myAudiChassisInputViewController .M文件

地方下面的代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)]; 

    UIBarButtonItem *itemOkey=[[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)]; 

    self.navigationItem.rightBarButtonItem=itemOkey; 
    self.navigationItem.leftBarButtonItem=retourButton; 
} 

我有如下的有效輸出,你想要enter image description here

希望它對你有幫助。

0

如果你有你的類的xib文件,那麼添加導航控制器並添加導航欄,並在其下添加UIBarButton。