Thursday, May 30, 2013

UIBarButtonItem initWithBarButtonSystemItem target action example in Objective C (iOS).


UIBarButtonItem initWithBarButtonSystemItem target action

Initializes a new item containing the specified system item.

- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action

Parameters
systemItem
The system item to use as the first item on the bar. One of the constants defined in UIBarButtonSystemItem.
target
The object that receives the action message.
action
The action to send to target when this item is selected.

Return Value of [UIBarButtonItem initWithBarButtonSystemItem target action]
A newly initialized item containing the specified system item. The item’s target is nil.

UIBarButtonItem initWithBarButtonSystemItem target action example.
-(void)viewDidload
{
app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];

}

-(IBAction)Add:(id)sender
{

    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];

}

Example of [UIBarButtonItem initWithBarButtonSystemItem target action].
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  
                         target:self
                         action:@selector(refreshAction)];

UIBarButtonItem initWithBarButtonSystemItem target action example.
static inline UIBarButtonItem *BarButtonWithSystemStyle(UIBarButtonSystemItem style, id target, SEL action)
{
    return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:style target:target action:action] autorelease];
}

static inline UIBarButtonItem *BarButtonWithFlexibleWidth(id target, SEL action)
{
    return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:target action:action] autorelease];
}

static inline UIBarButtonItem *BarButtonWithFixedWidth(CGFloat width, id target, SEL action)
{
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:target action:action];
    button.width = width;
    return [button autorelease];
}

End of UIBarButtonItem initWithBarButtonSystemItem target action example article.