Bar Button on navigation based app - iphone

I want to add a Bar button to the Navigation bar on a Navigation based app, the bar however is on the MainWindow and has no code behind so I Can't connect it to an IBAction.
how do i go about connecting it to an action ?

You can add the button to the navigation bar programmatically like this and also connect it up to an action with the init code:
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:<UIBarButtonStyle that you want> target:self action:#selector(<some_action>:)];
self.navigationItem.rightBarButtonItem = button;
[button release];

Related

Customize Navigation Controller

I have problem in Navigation Controller. I can't create dynamic buttons on Navigation Bar.
I have used Navigation Bar. Inside this, I have one TabBar.
And Inside TabBar, I have one more Navigation Controller.
I can create buttons on that. But I want Dynamic buttons on parent Navigation controller of TabBar.
For Button It's not working.
My Code:
UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(btnClick:)];
//[self.navigationItem setRightBarButtonItem:searchBtn];
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
For Hide the parent Navigation Controller, It's working.
app.navigationController.navigationBarHidden =YES;
How can I solve this problem.?
if i understand you correctly you should change this
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
into
[app.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];

Center a button in a navigationBar

I would like to center a button in my navigationBar, which is currently defined as a rightBarButtonItem :
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
You can use the .titleView property of your navigation item - but you'll need to create your own assets for this (you won't be able to use a UIBarButtonItem). From Apple's docs:
...(titleViews) can contain buttons. Use
the buttonWithType: method in UIButton
class to add buttons to your custom
view in the style of the navigation
bar. Custom title views are centered
on the navigation bar and may be
resized to fit.

Add a button to a navigation toolbar

How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.

How to add bar buttons to UINavigation bar

Hey, I am trying to add bar button items to my UINavigationBar (nav bar) but I have found out that bar button items are not properties of navigation bar and hence can't be accessed directly like:
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
navBar.rightBarButtonItem =rightButton;
I am creating my navigation bar programmatically.
Apparently, there is something called UINavigation Item that has to be dealt with to add bar buttons. Can anyone tell me how to go about this? How to create this UINavigation item programmatically and add this to my navBar and then add the bar buttons to it.
UINavigationBars shouldn't be used on their own, outside of a UINavigationController.
When a navigation controller is used, each view controller on the navigation stack has a UINavigationItem. It is this UINavigationItem that you can add bar button items to.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone
target:nil action:nil] autorelease];

How can I put an "info" button on the iPhone nav bar?

I want to put an 'i' button on the nav bar (on all the screens in which the nav bar appears). Touching on that button should bring up a view (perhaps modal) in which I can have controls. How do I put the button on the nav bar, and what how will I execute the call back?
Info Button Code:
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setLeftBarButtonItem:modalButton animated:YES];
[modalButton release];
The code above calls method -(void)infoButtonAction. Add this to all of your ViewControllers or just create one .nib and use that for all of your views.
You can also achieve it on storyboard.Drag a UIButton to you navigation bar instead of the usual UIBarButtonItem. You will see that your unbutton will be "nested" inside the bar button item which can be expanded.
Then you can change your button's type property to info light | dark from the attribute inspector.