2011-06-06 95 views
16

我正在嘗試此操作,但它不起作用。在導航欄上添加兩個以上的按鈕

-(void)viewDidLoad 
{ 
    // create a toolbar where we can place some buttons 
    UIToolbar* toolbar = [[UIToolbar alloc] 
        initWithFrame:CGRectMake(0, 0, 100, 45)]; 
    [toolbar setBarStyle: UIBarStyleBlackOpaque]; 

    // create an array for the buttons 
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

    // create a standard save button 
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
     target:self 
     action:@selector(saveAction:)]; 
    saveButton.style = UIBarButtonItemStyleBordered; 
    [buttons addObject:saveButton]; 
    [saveButton release]; 

    // create a spacer between the buttons 
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
     target:nil 
     action:nil]; 
    [buttons addObject:spacer]; 
    [spacer release]; 

    // create a standard delete button with the trash icon 
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] 
     initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
     target:self 
     action:@selector(deleteAction:)]; 
    deleteButton.style = UIBarButtonItemStyleBordered; 
    [buttons addObject:deleteButton]; 
    [deleteButton release]; 

    // put the buttons in the toolbar and release them 
    [toolbar setItems:buttons animated:NO]; 
    [buttons release]; 

    // place the toolbar into the navigation bar 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
      initWithCustomView:toolbar]; 
    [toolbar release]; 
} 

我該如何解決這個問題?

+0

有StackOverflow上在幾個類似的問題。看看[這些問題](http://stackoverflow.com/search?q=navigation+bar+multiple+items)。 – Piotr 2011-06-06 08:36:35

+0

我知道,但這個代碼不工作,這是問的問題? – Ajay 2011-06-06 08:41:24

+0

這是一個可可接觸的問題。所以請保留標籤。 – vikingosegundo 2011-06-06 08:46:05

回答

11
UIToolbar* toolbar = [[UIToolbar alloc] 
         initWithFrame:CGRectMake(0, 0, 320, 45)]; 
[toolbar setBarStyle: UIBarStyleBlackOpaque]; 

// create an array for the buttons 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:5]; 

// create a standard save button 
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
           target:self 
           action:@selector(deleteAction:)]; 
saveButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:saveButton]; 
[saveButton release]; 

// create a standard delete button with the trash icon 
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
           target:self 
           action:@selector(deleteAction:)]; 
deleteButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:deleteButton]; 
[deleteButton release]; 

UIBarButtonItem *addbutton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
           target:self 
           action:@selector(deleteAction:)]; 
addbutton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:addbutton]; 
[addbutton release]; 

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
           target:self 
           action:@selector(deleteAction:)]; 
editButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:editButton]; 
[editButton release]; 

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
           target:self 
           action:@selector(deleteAction:)]; 
doneButton.style = UIBarButtonItemStyleBordered; 
[buttons addObject:doneButton]; 
[doneButton release]; 

// put the buttons in the toolbar and release them 
[toolbar setItems:buttons animated:NO]; 
[buttons release]; 

// place the toolbar into the navigation bar 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar]; 
[toolbar release]; 

試試這個代碼片段

並在AppDelegate.h文件,你聲明這個

UINavigationController *navigationController; 

而且AppDelegate.m文件

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

// Override point for customization after application launch. 

// Add the view controller's view to the window and display. 
navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self.window addSubview:navigationController.view]; 
[self.window makeKeyAndVisible]; 

return YES; 
} 
+0

它不工作的朋友。我只是想簡單地在導航欄上添加按鈕。爲了做到這一點,我只是在瀏覽器上放置導航欄,然後放在視圖上。然後在word文件中添加此代碼.m文件 – Ajay 2011-06-06 09:06:46

+1

我發佈了代碼段。在那我添加了5個按鈕。看看這個。 :) – 2011-06-06 09:43:40

+0

我應該在哪裏編寫這段代碼 - (void)viewDidLoad {} – Ajay 2011-06-06 09:50:14

2

我想我給ü我的代碼,這將解決問題烏爾...

-(void) initializeNavigationalBar { 

self.navigationItem.title = @"What Ever"); 

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(57/255) green:(57/255) blue:(57/255) alpha:1]; 
UIBarButtonItem *optionBtn; 
optionBtn = [[UIBarButtonItem alloc] 
      initWithImage:[UIImage imageNamed:@"image1.png"] 
      style:UIBarButtonItemStylePlain 
      target:self 
      action:@selector(LoadOption:)]; 
self.navigationItem.leftBarButtonItem = optionBtn; 

// create a toolbar to have two buttons in the right 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 60, 44.01)]; 
// create the array to hold the buttons, which then gets added to the toolbar 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2]; 

UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] 
           initWithImage:[UIImage imageNamed:@"image2.png"] 
           style:UIBarButtonItemStylePlain target:self 
           action:@selector(decrement:)]; 

[buttons addObject:rightBtn]; 
[previousBtn release]; 
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] 
          initWithImage:[UIImage imageNamed:@"image3.png"] 
          style:UIBarButtonItemStylePlain target:self 
          action:@selector(increment:)]; 
[buttons addObject:leftBtn]; 
[nextBtn release]; 
[tools setItems:buttons animated:NO]; 
tools.tintColor = [UIColor colorWithRed:(57/255) green:(57/255) blue:(57/255) alpha:1]; 
[buttons release]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release];  

}

享受...

+0

感謝您的代碼。但是你正在做一個整數除法,所以按鈕總是黑色的。只需將255改爲255.0,就可以成爲一個浮動分區。 – Ricardo 2012-04-12 17:14:58

0

此代碼的工作well.just現在我檢查你的代碼。建設 - >乾淨的所有目標,然後檢查

+0

你好朋友。我可以在導航欄上的按鈕位置添加細分受衆羣 – Ajay 2011-06-06 09:41:40

+0

@ user6271是的確定要在導航欄上添加uisegmentedcontrol – Rams 2011-06-06 09:57:03

+0

如何在細分受衆羣按鈕1上單擊替換細分受衆羣按鈕1和其他細分受衆羣按鈕2。 – Ajay 2011-06-06 10:30:54

50

與iOS 5您可以添加更多按鈕

self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil]; 

同爲右按鈕

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil]; 
+0

感謝您的回答。爲我節省了很多時間! – Varuna 2012-02-25 08:06:24

+0

是的..無限期地節省了很多時間... +1 – Aditya 2012-08-14 06:22:47

+0

+1。謝謝。這很快實施! – akshay1188 2012-08-16 08:21:23