How to create three buttons in UINavigationbar in iPhone programmatically? - iphone

I'm developing a small application. I need to create three buttons in a subclass. One button is add, another one is search and the last one is back. I also create left and right buttons. But I can't create search button in the center of the navigation bar. How can I create it? My code is:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
[flipButton release];
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addbuttonview)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
}
How to create a middle button in the navigation bar? Please help me.

Below is the code to use segment control in navigation bar programatically
NSArray* arr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"Log_Button.png"], [UIImage imageNamed:#"Chart_Button.png"], nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:arr];
[segmentedControl addTarget:self action:#selector(action) forControlEvents:UIControlEventValueChanged];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[arr release];
UIBarButtonItem *rb = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rb];
[rb release];

UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10, 4, 100, 50);
[btnBack setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"button_back" ofType:#"png"]] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnBack];
UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
btnHome.frame = CGRectMake(115, 4, 38, 30);
[btnHome setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"header_icon_home" ofType:#"png"]] forState:UIControlStateNormal];
[btnHome addTarget:self action:#selector(btnHomePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnHome];
UIButton *searchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame=CGRectMake(175, 2, 60, 40);
[searchBtn addTarget:self action:#selector(seachbtnPressed:) forControlEvents:UIControlEventTouchDown];
[searchBtn setImage:[UIImage imageNamed:#"Search.jpg"] forState:0];
[self.navigationController.navigationBar addSubview:searchBtn];
take three UIButtons and Add to navigationBar.set Frames Images As Per Ur Design.

Related

How to add 2 buttons on navigation bar?

I am using Split View Controller, which has 2 View controllers on the second view controller i am suppose add two buttons on the right side of navigation controller.
i have used the following code to add one button which works:
UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
[barButton setCustomView:btnShare];
self.navigationItem.rightBarButtonItem=barButton;
tried this link http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html
but could not succeed.Please help me fix this.
Try this
NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init];
UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSetting setImage:[UIImage imageNamed:#"settings.png"] forState:UIControlStateNormal];
btnSetting.frame = CGRectMake(0, 0, 32, 32);
btnSetting.showsTouchWhenHighlighted=YES;
[btnSetting addTarget:self action:#selector(onSettings:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting];
[arrRightBarItems addObject:barButtonItem];
UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLib setImage:[UIImage imageNamed:#"library.png"] forState:UIControlStateNormal];
btnLib.frame = CGRectMake(0, 0, 32, 32);
btnLib.showsTouchWhenHighlighted=YES;
[btnLib addTarget:self action:#selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib];
[arrRightBarItems addObject:barButtonItem2];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRefresh setImage:[UIImage imageNamed:#"refresh.png"] forState:UIControlStateNormal];
btnRefresh.frame = CGRectMake(0, 0, 32, 32);
btnRefresh.showsTouchWhenHighlighted=YES;
[btnRefresh addTarget:self action:#selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh];
[arrRightBarItems addObject:barButtonItem1];
self.navigationItem.rightBarButtonItems=arrRightBarItems;
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"btn_home" heighlightImageName:#"btn_home_h" buttonFrame:CGRectMake(2, 1, 34, 34) selectorName:#selector(buttonHomeClicked:) target:self]]];
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setFrame:CGRectMake(240, 5, 34, 34)];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
[tempButton setImage:[self getImageFromResource:#"btn_fav_h"] forState:UIControlStateSelected];
[self.navigationController.navigationBar addSubview:self.buttonFavorite];
create two uibarbuttonitem and add both in an array then add whole array in navigation bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addButton,sendButton];
try thi code
UIButton *btnLogOut = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogOut.frame = CGRectMake(0, 0, 62, 31);
[btnLogOut setImage:[UIImage imageNamed:#"logout_new.png"] forState:UIControlStateNormal];
[btnLogOut addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLogOut];
UIButton *btnError = [UIButton buttonWithType:UIButtonTypeCustom];
btnError.frame = CGRectMake(0, 0, 62, 31);
[btnError setImage:[UIImage imageNamed:#"list-icon.png"] forState:UIControlStateNormal];
[btnError addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *errorButton = [[UIBarButtonItem alloc] initWithCustomView:btnError];
NSArray *navigationBarBtnArray=[NSArray arrayWithObjects:errorButton,logOutBarButton, nil];
self.navigationItem.rightBarButtonItems=navigationBarBtnArray;
Try this
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
make a UIView adding as many buttons you want on it. then use
UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView];
self.navigationItem.rightBarButtonItem = myBarbtn;
Use segmented control for this
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl setMomentary:YES];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample1.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample2.png"] atIndex:1 animated:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
Make an array of UIBarButtonItems and pass it to
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
method of UINavigationItem.
Simply use an UISegmentedControl instead of a UIButton.
Then change the mode to "momentary" and add an action on change.
When the action is triggered, check the selectedSegmentIndex to know what segment was chosen.

How to make visible a leftBarButtonItem created programmatically and set backgroundimage

Below code is working fine in my application. But that leftbarbuttonitem is invisible. And I can't set background image to leftBarButtonItem. I did some thing wrong, but I don't know what is the mistake? Is it possible to make visible and set background image?
UIImageView *NavigationBarImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
NavigationBarImage.image=[UIImage imageNamed:#"mapbutton.png"];
[self.navigationController.navigationBar addSubview:NavigationBarImage];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
-(void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
If you want to add a backgroundImage to your navigation item you have to add a custom button, like bellow:
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
tempButton.frame = CGRectMake(0, 0, 50, 30);
tempButton.showsTouchWhenHighlighted = YES;
tempButton.backgroundColor = [UIColor clearColor];
[tempButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"Back" forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithCustomView:tempButton];
self.navigationItem.leftBarButtonItem = backBtn;
And to add a background Image to navigation bar you can use the following code
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:#"YOUR_IMAGE.png"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
[home addTarget:self action:#selector(your_Action)
forControlEvents:UIControlEventTouchUpInside];
home.frame = CGRectMake(0, 0, 69, 26);
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:home];
[[self navigationItem] setLeftBarButtonItem:button2];
[button2 release];
button2 = nil;

UIToolbar as right button in a UINavigationbar, contain 2 buttons without spaced between them

I wanted to make my right bar button contain 2 buttons. So I did that by using a UIToolbar. But the problem is that the 2 buttons sit apart from each other, while the affect I would like to achieve is too have them sitting flush against each other.
Here is an image of how they look now
Here is the code I use to achieve the buttons so far
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 2.0f, 120.0f, 40.01f)]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = YES;
tools.tintColor = [UIColor blueColor];
tools.barStyle = -1;// -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIButton * upButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
upButton.frame = CGRectMake(0, 07, 46, 30);
upButton.titleLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:16];
[upButton setTitle:#"" forState:UIControlStateNormal];
upButton.backgroundColor = [UIColor clearColor];
[upButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[upButton addTarget:self action:#selector(pageDown) forControlEvents:UIControlEventTouchUpInside];
[upButton setBackgroundImage:[UIImage imageNamed:#"page_up.png"] forState:UIControlStateNormal];
[upButton setBackgroundImage:[UIImage imageNamed:#"page_up_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemOne = [[UIBarButtonItem alloc] initWithCustomView:upButton];
[buttons addObject:toggleSegmentedControlBarItemOne];
UIButton * downButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
downButton.frame = CGRectMake(0, 07, 46, 30);
downButton.titleLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:16];
[downButton setTitle:#"" forState:UIControlStateNormal];
downButton.backgroundColor = [UIColor clearColor];
[downButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[downButton addTarget:self action:#selector(pageUp) forControlEvents:UIControlEventTouchUpInside];
[downButton setBackgroundImage:[UIImage imageNamed:#"page_down.png"] forState:UIControlStateNormal];
[downButton setBackgroundImage:[UIImage imageNamed:#"page_down_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemTwo = [[UIBarButtonItem alloc] initWithCustomView:downButton];
[buttons addObject:toggleSegmentedControlBarItemTwo];
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
Can anybody please advise me how to get those two buttons to sit beside each other without any gap?
Many Thanks,
-Code
You can add UISegmentedControl as customView to your navigationItem and set its momentary property to true, that way you will get exactly what you want.
Here is sample code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Test";
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
segControl.momentary = YES;
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.tintColor = [UIColor darkGrayColor];
[segControl insertSegmentWithImage:[UIImage imageNamed:#"up.png"] atIndex:0 animated:NO];
[segControl insertSegmentWithImage:[UIImage imageNamed:#"down.png"] atIndex:1 animated:NO];
[segControl addTarget:self action:#selector(segButtonDown:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
}
- (void)segButtonDown:(id)sender {
UISegmentedControl *segControl = (UISegmentedControl *)sender;
switch (segControl.selectedSegmentIndex) {
case 0:
NSLog(#"Up");
break;
case 1:
NSLog(#"Down");
default:
break;
}
}
And here is image how it looks like.
And here you can see events in console.
2012-11-25 16:03:47.805 test2[13886:c07] Up
2012-11-25 16:03:48.367 test2[13886:c07] Down
2012-11-25 16:03:48.930 test2[13886:c07] Up
2012-11-25 16:03:49.538 test2[13886:c07] Down
take a look at this tutorial, maybe it helps :
Xcode: Multiple Buttons in Nav Bar
Add a no space space between two buttons.
two do this.
UIBarButtonItem *noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
noSpace.width = -10.0;
insert this noSpace objet to you'r array (buttons in your case). between two objects of UIBarButtonItem.
it'l be ok..

how to display the go back style UIBarButtonItem programmatically

When One of my app navigate from one controller to another, the 'go back' UIBarButtonItem disappear, so I wrote codes:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(barButtonItemPressed:)];
self.navigationItem.leftBarButtonItem = barButton1;
[barButton1 setImage:[UIImage imageNamed:#"fdj.png"]];
[barButton1 release];
It works, but it display as
but I prefer the style (like standard go back barbuttonitem
Is it possible?
Welcome any comment
You should edit the navigationItem of the parent view controller:
You can place this in the viewDidLoad of the previous view controller:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:#"fdj.png"];
here is the code how you can do this
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 55, 36);
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateNormal];
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = item4;
just make image and set it in uibutton, it's simple way to do this.
you can set any image you want.
hope this may help you what you want.
In the parent view controller's init method, set
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIimage imageNamed: #"fdj.png"]
style: UIBarButtonItemStyleBordered
target: nil
action: nil] autorelease];
This is what I'm using in an older project of mine (and yes, I know that stretchableImageWithLeftCapWidth is deprecated).
#implementation UIViewController (UIView_ExtenderClass) // this is a category, obviously
...
-(void)setLeftButton:(id)theTarget navItem:(UINavigationItem *)myNavItem action:(SEL)myAction title:(NSString *)myTitle;
{
CGSize titleWidth = [myTitle sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleWidth.width + 20, 32)];
[button setTitle: [NSString stringWithFormat:#" %#", myTitle] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
[button addTarget:theTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
UIImage *imgBack = [UIImage imageNamed:#"32_left_nav_bar.png"];
UIImage *imgBackStretched = [imgBack stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *imgBackSelected = [UIImage imageNamed:#"32_left_nav_bar_selected.png"];
UIImage *imgBackSelectedStretched = [imgBackSelected stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button setBackgroundImage:imgBackStretched forState:UIControlStateNormal];
[button setBackgroundImage:imgBackSelectedStretched forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttonItem setTarget:theTarget];
myNavItem.leftBarButtonItem = nil;
myNavItem.leftBarButtonItem = buttonItem;
[imgBack release];
[imgBackSelected release];
[button release];
[buttonItem release];
}
Call it in the viewDidLoad of the second controller, like this:
[self setLeftButton:self navItem:self.navigationItem action:#selector(yourActionHere) title:#"Back"]
The image I'm using is here: http://i49.tinypic.com/6qfzfs.png

UIButton as UINavigationbar button

I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.