Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding more than two button on the navigationbar

I am trying this but it does not work.

-(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];
}

How can I solve this?

like image 496
Ajay Avatar asked Jun 06 '11 08:06

Ajay


People also ask

How do I add more buttons to my navigation bar?

From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.

How to create a more button in html?

Example ExplainedUse any element to open the dropdown menu, e.g. a <button>, <a> or <p> element. Use a container element (like <div>) to create the dropdown menu and add the dropdown links inside it. Wrap a <div> element around the button and the <div> to position the dropdown menu correctly with CSS.


3 Answers

with iOS 5 you can add more buttons

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

same for right buttons

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:uibarbuttonInstance1, uibarbuttonInstance2, nil];
like image 147
palaniraja Avatar answered Oct 19 '22 03:10

palaniraja


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];

Try this code snippet

And In AppDelegate.h file you declare this

UINavigationController *navigationController; 

And AppDelegate.m file

- (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;
}
like image 25
Naveen Thunga Avatar answered Oct 19 '22 02:10

Naveen Thunga


I think i'm giving u my code this will solve ur problem ...

-(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];    

}

Enjoy...

like image 27
user755278 Avatar answered Oct 19 '22 03:10

user755278