Friday, May 31, 2013

UIBarButtonItem initWithTitle style target action example in Objective C (iOS).


UIBarButtonItem initWithTitle style target action

Initializes a new item using the specified title and other properties.

- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

Parameters of [UIBarButtonItem initWithTitle style target action]
title
The item’s title. If nil a title is not displayed.
style
The style of the item. One of the constants defined in UIBarButtonItemStyle.
target
The object that receives the action message.
action
The action to send to target when this item is selected.

Return Value of [UIBarButtonItem initWithTitle style target action]
Newly initialized item with the specified properties.

UIBarButtonItem initWithTitle style target action example.
UIBarButtonItem *buttonHello = [[UIBarButtonItem alloc] initWithTitle:@"Say Hello"    
    style:UIBarButtonItemStyleBordered target:self action:@selector(sayHello:)];

Example of [UIBarButtonItem initWithTitle style target action].
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];

// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];

UIBarButtonItem initWithTitle style target action example.
UIToolbar* toolbar = [[UIToolbar alloc]
                  initWithFrame:CGRectMake(0, 0, 320, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

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

// create a clearAll button
UIBarButtonItem * clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear All"
                             style:UIBarButtonItemStylePlain
                             target:self
                             action:@selector(clearAllAction:)];

clearAllButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:clearAllButton];
[clearAllButton release];

 // create a calculate button
 UIBarButtonItem *calculateButton = [[UIBarButtonItem alloc]initWithTitle:@"Calculate"
                             style:UIBarButtonItemStylePlain
                           target:self
                           action:@selector(calculateButton:)];
calculateButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:calculateButton];
[calculateButton release];

// create a settingButton
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]initWithTitle:@"Setting"
                             style:UIBarButtonItemStylePlain
                             target:self
                             action:@selector(settingButton:)];
settingButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:settingButton];
[settingButton release];

 // create a buyNowButton

UIBarButtonItem *buyNowButton = [[UIBarButtonItem alloc]initWithTitle:@"Buy Now"
                             style:UIBarButtonItemStylePlain
                          target:self
                          action:@selector(buyNowButton:)];
buyNowButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:buyNowButton];
[buyNowButton release];

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

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

End of UIBarButtonItem initWithTitle style target action example article.