1

我想在我的應用程序中使用NavigationController的工具欄。這個工具欄的toolbarItems假設根據呈現的視圖控制器而改變。這是非常基本的。uinavigationController與自定義項目的工具欄

我想要做的是使用UIBarButtonItem的「initWithCustomView:」方法將自定義按鈕添加到工具欄。但是,該按鈕不會顯示在工具欄上。但是,如果我使用「initWithTitle:」或「initWithBarButtonSystemItem:」方法創建UIBarButtonItem,則會顯示按鈕。例如,請看下面的代碼:

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil]; 

NSArray *array = [[NSArray alloc] initWithObjects:item1, item2, nil]; 
[self setToolbarItems:array]; 

如果這樣做,按鈕顯示在工具欄上。但是,如果我要執行以下操作:

UIButton* replyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal]; 
[replyBtn addTarget:self action:@selector(replyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
replyBtn.frame = CGRectMake(10, 0, 40, 40); 
UIBarButtonItem *replyButton = [[UIBarButtonItem alloc] initWithCustomView:replyBtn]; 

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:self action:nil]; 
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"edit" style:UIBarButtonItemStylePlain target:self action:nil]; 

NSArray *array = [[NSArray alloc] initWithObjects:item1, replyButton, item2, nil]; 
[self setToolbarItems:array]; 

在此代碼中,只有item1和item2顯示在工具欄上。 replyButton不顯示在工具欄上。假設按鈕所在的地方有空白處。

如果在我創建的常規工具欄上使用相同的代碼而不是NavigationController的工具欄,該按鈕將顯示出來。我正試圖在整個應用程序中使用一個工具欄,以便與Apple的Mail應用程序具有相同的感覺。我需要使用「initWithCustomView:」方法的原因是因爲其中一個圖標是彩色的,這是它在常規工具欄上顯示的唯一方式。現在,我瀏覽了蘋果文檔,並沒有提到爲什麼不能調用「initWithCustomView:」方法(或者我找不到它)。

可以請某人在這個話題上閃耀一些光,幫助我指出正確的方向。在此先感謝你們。

+0

有相同的問題。我不知道爲什麼它不工作......我可能還需要添加一個不同的工具欄「添加子視圖」。 – 2011-03-30 07:25:22

回答

1

我不能看到你嘗試過什麼不同,但它最終爲我工作,與該代碼:

////////////////////////////////////////////////////////////////////////////////////////////// 
/////>>>> Adding buttons for browsing in the toolbar of the popover's navigation controller/// 
////////////////////////////////////////////////////////////////////////////////////////////// 

//>>>>Create a goBack button  
goBack = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *goBackImage = [UIImage imageNamed:@"back1.png"]; 
[goBack setImage:goBackImage forState:UIControlStateNormal]; 
[goBack addTarget:self action:@selector(goBackClicked:) forControlEvents:UIControlEventTouchUpInside]; 
goBack.frame = CGRectMake(0,0,goBackImage.size.width,goBackImage.size.height); 
//Create a Bar button to hold this button 
UIBarButtonItem *goBackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:goBack] autorelease]; 

UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; 
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:nil action:nil] autorelease]; 
NSArray *arrayOfButtons = [[[NSArray alloc] initWithObjects:goBackBarButton, flex1, addButton, nil] autorelease]; 
[self setToolbarItems:arrayOfButtons]; 

注意從你的一些差異(也許這也正是抓我?不敢肯定): 1.我的按鈕沒有在本地方法分配,但在類(你知道,房地產,綜合等) 2.你

[replyBtn setImage:[UIImage imageNamed:@"Reply_Message.png"] forState:UIControlStateNormal]; 

其中m INE看起來有點不同

[goBack setImage:goBackImage forState:UIControlStateNormal]; 

嘗試這些微小的變化,也許它會工作:)

0

我敢打賭,該項目沒有顯示,因爲它的視圖沒有任何內容。你確定在你的項目中有一個叫做Reply_Message.png的圖片嗎? [UIImage imageNamed:@"Reply_Message.png"]可能是nil

+0

我對此很樂觀。如果我採用相同的確切代碼並將這些項目設置爲我創建的工具欄,則會顯示該按鈕。這確實不是問題。我甚至試過一個UIButton只有一個標題,而不是圖像,添加作爲一個子視圖到的UIBarButtonItem,甚至不會出現。 – Bittu 2010-07-28 03:29:09

0

您可以在整個應用程序中使用一個工具欄,而無需使用導航控制器工具欄。由於您的代碼適用於非導航控制器工具欄,因此這可能是實現您想要的最簡單的方法。在你的應用程序委託中,嘗試添加一個工具欄到窗口,就像這樣。這樣它就貫穿整個應用程序。確保你考慮一個方案,讓你訪問工具欄添加/刪除按鈕,或隱藏/顯示它從不同的視圖控制器。

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    /////// frame sizes //////// 
    // compensate for the status bar 
    int statusBarHeight = 20; 

    // toolbar 
    int toolbarWidth = window.frame.size.width; 
    int toolbarHeight = 30; // I think standard size is 30 
    int toolbarxOffset = 0; 
    int toolbaryOffset = window.frame.size.height - tHeight; 
    CGRect toolbarFrame = CGRectMake(toolbarxOffset, 
            toolbaryOffset, 
            toolbarWidth, 
            toolbarHeight); 

    /////// toolbar ////////////////////// 
    self.myPersistentToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame]; 
    [window addSubview:self.myPersistentToolbar]; 
} 
+2

謝謝你的回覆德魯。我早就想到了這個想法。如果我有一個使用navigationController作爲它的rootViewController的應用程序,這可能已經奏效。相反,我使用這個Three20庫的啓動器作爲根視圖,每個「子應用程序」包含它自己的設置。有些在其中有tabbars,而另一些則有navigationController或splitviewController。由於這個原因,我試圖避免向窗口添加任何東西。我想出的解決方案是將我自己的工具欄添加爲navigationController視圖的子視圖。 – Bittu 2010-08-20 02:47:45

1

我有同樣的問題。事實證明,UINavigationController的工具欄在每次將新視圖壓入堆棧時重置它的項目。我試圖設置applicationDidFinish函數中的工具欄項目,並且它不工作。它工作了一次,我把工具欄設置在被推入導航堆棧的viewController的 - (void)viewDidAppear函數中。

所以,好像如果你想要導航控制器保持相同的工具欄項目在整個應用程序,你必須設置在你推到導航控制器出現視圖後,每個視圖工具欄項目。

我希望有幫助!