Thursday, May 30, 2013

UIBarButtonItem initWithBarButtonSystemItem example in Objective C (iOS).


UIBarButtonItem initWithBarButtonSystemItem

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]
A newly initialized item containing the specified system item. The item’s target is nil.

UIBarButtonItem initWithBarButtonSystemItem 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].
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  
                         target:self
                         action:@selector(refreshAction)];

UIBarButtonItem initWithBarButtonSystemItem 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 example article.