1

how do I add by code on a button to a toolbar that I have the property?

@property (strong, nonatomic) IBOutlet UIToolbar *toolB;
John Riselvato
  • 12,854
  • 5
  • 62
  • 89
Antilope
  • 443
  • 2
  • 6
  • 17
  • can you explain a little more? – John Riselvato May 24 '12 at 14:22
  • I want to code to decide whether to add a button to a view. I thought about creating a "UIViewController" and put on a "UIToolBar" but I do not know how to add by code a button on the "UIToolBar" (positioned right). – Antilope May 24 '12 at 14:33

2 Answers2

11
UIBarButtonItem *buttonOne = [[UIBarButtonItem alloc] initWithTitle:@"Button One" style:UIBarButtonItemStyleBordered target:self action:@selector(action)];

UIBarButtonItem *buttonTwo = [[UIBarButtonItem alloc] initWithTitle:@"Button Two" style:UIBarButtonItemStyleBordered target:self action:@selector(action)];

NSArray *buttons = [NSArray arrayWithObjects: buttonOne, buttonTwo, nil];
[toolBar setItems: buttons animated:NO];

Will do the trick if i understand correctly what you are asking. action being the method you want the buttons to call.

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • I have two questions: 1) if I added the "UIBarButtonItems" and "UILabel" from the storyboard, can I move them all right? 2) can I add "UILabel" into array? – Antilope May 24 '12 at 15:45
  • 1
    take a look at this: http://stackoverflow.com/questions/602717/aligning-uitoolbar-items – John Riselvato May 24 '12 at 16:04
0

This may help you, dont forget to release Buttons.

UIToolbar *actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
UIBarButtonItem *actionButton =
    [[[UIBarButtonItem alloc]
        initWithTitle:@"No Action"
        style:UIBarButtonItemStyleBordered
        target:self
        action:@selector(noAction:)]
    autorelease];
[actionToolbar setItems:[NSArray arrayWithObject:actionButton]];

UIToolbar dont have side buttons, you can use UINavigationBar or follow this link

Aligning UIToolBar items

Community
  • 1
  • 1
ymutlu
  • 6,585
  • 4
  • 35
  • 47