add button to navigationbar programatically

Posted by mac on Stack Overflow See other posts from Stack Overflow or by mac
Published on 2010-05-17T09:55:31Z Indexed on 2010/05/31 8:52 UTC
Read the original article Hit count: 411

Hi i need to set the button on right side, in navigation bar, programatically , so that if i press the button i will perform some actions,

i have created the navigation bar,programatically by navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];

similarly i need to add the button , right side of this navigation bar,for that i used,

1) UIView* container = [[UIView alloc] init];

// create a button and add it to the container
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)];
[container addSubview:button];
[button release];

// add another button
button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)];
[container addSubview:button];
[button release];

// now create a Bar button item
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
[item release];

2) UIImage *im;

im=[UIImage imageNamed:@"back.png"];
[button setImage:im forState:UIControlStateNormal];
[im release];
backButton = [[UIBarButtonItem alloc] initWithCustomView:button];       
[backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)];
[self.navigationItem setRightBarButtonItem:backButton];

3) UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];

self.navigationItem.rightBarButtonItem = refreshItem;

[refreshItem release];

i tried all these type of codings , no use, these codes are not displaying the button at rigth side,please help me.

thanks in advance,

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk