2013-05-06 26 views
1

我在我的TabBarView中有一個導航欄。我想要一個rightBarButtonItem和我的導航欄上的標題。這是爲什麼發生?我的代碼在viewDidLoad方法中。爲什麼我的標題顯示爲左鍵項目?然後當我點擊它時,它就像移動了一個視圖,然後我的右欄按鈕項消失了,標題正確。爲什麼我的標題顯示爲左鍵?

UINavigationBar *myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320,40)]; 
myNavBar.barStyle = UIBarStyleBlackOpaque; 

[self.view addSubview:myNavBar]; 

//Creates the title. 
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"My Title"]; 

[myNavBar pushNavigationItem:title animated:NO]; 

//Creates the button. 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self 
action:@selector(showMail:)]; 

UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil]; 
navItem.rightBarButtonItem = button; 

// add the UINavigationItem to the navigation bar 
[myNavBar pushNavigationItem:navItem animated:NO]; 
+0

爲什麼會發生什麼? – 2013-05-06 20:31:52

+0

問題的標題說最新發生的笑。也許我應該在那裏包含它。爲什麼我的標題顯示爲左鍵項目?然後當我點擊它時,它會動作,它正在移動一個視圖,然後我的右側欄按鈕項目消失,標題正確。 – Jason 2013-05-06 20:34:59

回答

4

UINavigationItem包含標題和欄按鈕項目。您正在創建多個navigationItems,而您應該只分配一個UINavigationItem的屬性。

看看頭文件,你會看到它包含了你所需要的一切:

NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationItem : NSObject <NSCoding> { 
@private 
    NSString  *_title; 
    NSString  *_backButtonTitle; 
    UIBarButtonItem *_backBarButtonItem; 
    NSString  *_prompt; 
    NSInteger  _tag; 
    id    _context; 
    UINavigationBar *_navigationBar; 
    UIView   *_defaultTitleView; 
    UIView   *_titleView; 
    UIView   *_backButtonView; 
    NSArray   *_leftBarButtonItems; 
    NSArray   *_rightBarButtonItems; 
    NSArray   *_customLeftViews; 
    NSArray   *_customRightViews; 
    BOOL    _hidesBackButton; 
    BOOL    _leftItemsSupplementBackButton; 
    UIImageView  *_frozenTitleView; 
} 

就分配它的一個實例。你不應該分配多個UINavigationItems。

+0

你能更具體嗎? – Jason 2013-05-06 20:42:59

+0

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationItem_Class/Reference/UINavigationItem.html – 2013-05-06 20:44:21

+0

你創建已經內置到UINavigationItem BarButtons的實例。 – 2013-05-06 20:44:52

0

您正在創建多個導航按鈕。您應該將所有這些屬性設置爲一個按鈕。如果你想爲欄設置標題,你可以將它設置爲self.title = @「title」;

您還可以嘗試設置navigationbar.titleview。

+0

以上的代碼,如果我嘗試self.title = @「my title」;它根本沒有出現。 – Jason 2013-05-06 20:39:34

+0

我試過標題,titleview,topitem.title所有這些,標題不顯示任何。 – Jason 2013-05-06 20:42:37

+0

然後創建UILabel並將其添加爲myNavBar的子視圖。 – user 2013-05-06 20:44:06