2011-10-12 124 views
2

使用下面的代碼我正在實現我自己的導航欄。出於某種原因,當我運行我的應用程序時,導航欄上沒有顯示後退(左箭頭)按鈕。但是,如果我將代碼更改爲leftBarButtonItem,則按鈕確實會出現。UINavigationItem BackButtonItem not displayed

// Draw Navigation Bar 
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[navigationBar setDelegate:self]; 

UINavigationItem *navigationItem = [[UINavigationItem alloc] init]; 

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                   style:UIBarButtonItemStylePlain 
                   target:nil 
                   action:nil]; 
navigationItem.backBarButtonItem = backButton; 
[navigationBar pushNavigationItem:navigationItem animated:NO]; 
[self.view addSubview:navigationBar]; 
[navigationBar release]; 
[backButton release]; 

回答

1

想通了:

// Draw Navigation Bar 
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[navigationBar setDelegate:self]; 

UINavigationItem *navigationItem = [[UINavigationItem alloc] init]; 

UIButton *button = [UIButton buttonWithType:101]; 
// add selector 
[button setTitle:@"Back" forState:UIControlStateNormal]; 

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 

navigationItem.leftBarButtonItem = backButton; 
[navigationBar pushNavigationItem:navigationItem animated:NO]; 
[self.view addSubview:navigationBar]; 
[navigationBar release]; 
[backButton release]; 
3

backBarButtonItem由父級ViewController設置。 換句話說,它不是由您看到它的ViewController設置的,而是由它指向的ViewController設置的。所以如果你的ViewController是第一個,它就不會有後退按鈕。

此外,你正在自己創建一個NavigtionBar。這通常不是要走的路,UINavigationBar不是像按鈕或標籤那樣的UI元素。您應該使用UINavigationController來處理ViewControllers的所有推送和彈出。

+0

我明白了,一般像你說我會實現導航欄。然而,在這種情況下,我用程序創建了一個SplitView,這意味着我的SplitViewController是UINavigationController。我想要做的就是隱藏SplitView的NavigationBar併爲我的左側和右側ViewController構建獨特的NavigationBar –