Add more then one button in top navigation bar in iOS - ios

I am currently developing a chat application and within this I have to add more then 1 left bar button in navigation bar. I am developing it using xib file.
I already added left and right bar button now I want to add 2 more in the left side of navigation bar.

UINavigationItem Class Reference having property of leftBarButtonItems and rightBarButtonItems so you can add multiple UIBarButtonItem in your navigation control using following example code:
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:#"First"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(AcionFirst:)];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc]
initWithTitle:#"Second"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(AcionSecond:)];
self.navigationItem.leftBarButtonItems= [NSArray arrayWithObjects:FirstButton,secondButton,nil];
-(void)AcionFirst:(id)sender
{
//do something for first bar button
}
-(void)AcionSecond:(id)sender
{
//do something for second bar button
}

Related

Xcode Master Detail Application back button properties

How do I add properties for the "back button" in the navigation bar? The button links from detail view and back to the master view. I can't find any code for the back button, and the button is hidden in storyboard view.
This back button is added automatically by the UINavigationController in iOS, you can customize some things like the name, e.g. if you want to change the title or remove it totally while keeping the icon, you can use the below code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Custom Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Leave the title #"" if you want no text; also you can customize the behavior of the button by adding your own method like this:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Custom Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonEvent)];
- (void)backButtonEvent {
// Your code here
}
Be careful, this should be added to viewDidLoad in the first controller, not the second one.

How do I change the Labels on all three buttons on a UINavigationBar

So, I have the below UINavigationBar :
[ (<- Back) Info (Cancel) ]
Now, what I need to do is change the Titles for all the Buttons / Title to One, Two and Three. I tried doing :
self.navigationController.navigationBar.topItem.title = #"One";
But this only updated the button on the left. How do I change all three?
self.navigationItem.title will change the center title, and setting a button with title to self.navigationItem.rightBarButtonItem will change the right button title.
And here's how I've changed the back button title. Note: this code must be used on the previous view controller (the one you'd be hitting the back button to return to), not the presently viewed controller:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}

Adding a navigation bar button

I am trying to add a button to the navigation bar in the view, but it is not showing up.
I have:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"audio-controls"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlaybackView:)];
in a viewDidLoad.
EDIT: It is also important to note that I am also using storyboard.
EDIT: I think there is an issue with self.navigationItem... it's not nil but no changes are being made to it.

UIBarButton added to UINavigationItem not showing up

I am trying to add a bar button to my iOS app and can't get it to show up. I can see the bar in the Navigation Item's view hierarchy by setting a breakpoint. If it helps, I chose 'Embed Navigation Controller'. Any idea what's going on?
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(choosePreferredTerm:)];
[self.navItem setRightBarButtonItem:item animated:YES];
Here is the connection in IB
This is what the embedded Navigation Controller looks like:
This is what it looks like on the sim:
Try this rather than using custom outlet "navItem" also insure your application has a navigation controller in place
self.navigationItem.rightBarButtonItem = barButton;

UIBarButton won't show in my UINavigationController

I want to make a simple view with a navigation bar and a table view. So I created a subclass of UINavigationController and I added a UITableView in it. My is is also the UITableViewDataSource and UITableViewDelegate. Now I want to add UIBarButton in the navigation bar but it doesn't work :
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:[[Translator instance] getTranslation:#"Back"]
style:UIBarButtonItemStylePlain
target:self.parentViewController
action:#selector(dismissFiltersModal:)];
self.navigationItem.backBarButtonItem = anotherButton;
There's no error the view is displayed but not the button. How can I do to display it?
jafar,
as Apple doc suggested you should avoid to subclass UINavigationController.
This class is not intended for subclassing.
Said this, you could follow these simply steps (some advice for you):
Create your controller (say MyController) that extends UIViewController and add the table here. Set your controller as the delegate and data source for that table. If you have created by xib you need an outlet for your table view.
[P.S. you could also subclass a UITableViewController, it has a table view its own]
In viewDidLoad of MyController customize the navigation bar.
For example
UIBarButtonItem* myButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(someSelector)];
self.navigationItem.leftBarButtonItem = myButton;
You could also customize the title.
For example
self.title = #"Your custom title";
Somewhere in your code create a UINavigationController and add as a root view controller an instance of MyController.
For example:
MyController* myCtr = // alloc-init here
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:myCtr];
The backBarButtonItem should be set on the previous view controller, not the current one to be displayed. For example, if your you are on view A, navigate to B from it, inside B A's backBarButtonItem will be displayed, not B's.
If you just want to display an item on the left hand side of the navigation bar, you can use leftBarButtonItem instead, which will display a normal bar button on that controller. It will be rectangular, not an arrow like normal back buttons though.
How about you try this :
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:#selector(dismissFiltersModal:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
// create button item --
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add to toolbar
self.navigationItem.leftBarButtonItem = backItem;

Resources