How to add a right bar button to a navigation bar in iphone - ios

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.
I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:
-(void)viewDidload{
self.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];
}

Let assume, you have a UIViewController, like this:
UIViewController *vc = [UIViewController new];
And you added it to navigation controller:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
In this way rightBarButtonItem will NOT getting displayed:
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
BUT this way IT WILL appear:
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.

-(void)viewDidLoad
{
[super 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];
}
Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.

Add this code in viewdidload
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:#"Calculate" style:UIBarButtonItemStylePlain target:self action:#selector(nextview:)];
self.navigationItem.rightBarButtonItem = chkmanuaaly;

Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).
// from within self (aka the calling controller):
UIViewController *c = [[[UIViewController alloc] init...] autorelease];
c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissModalViewControllerAnimated:)] autorelease];
c.navigationItem.title = #"Title Goes Here";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
nav.navigationBarHidden = FALSE;
[self presentModalViewController:nav animated:YES];
Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)

UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];

I guess it will be more complete response and it should help in any situation:
-(void)viewDidLoad{
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = #"Title";
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
//Add selector to info button
[tempButton addTarget:self action:#selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = #[navTitle];
[self.view addSubview:navBar];
}

Use the following code:
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Hope this helps you out.
Enjoy!

Swift:
let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
style: .plain,
target: self,
action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)
#objc func buttonTapped(sender: UIBarButtonItem!) {
// Implement action
}

UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);
[dButton addTarget:self action:#selector(clickdButton:)
forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:#"iconnavbar.png"]
forState:UIControlStateNormal];
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];
UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;
and then:
-(IBAction)clickdButton:(id)sender{
classexample *tempController=[[classexample alloc] init];
[self.navigationController pushViewController:tempController animated:YES];
[tempController autorelease];
}

Related

UInavigatonbar items

I add a uinavigationcontroller as a subview on a uiviewcontroller (rootviewcontroller of aap) and this view controller is a subview as a rootviewcontroller on self.window.
in Appdelegate I use:
self.window.rootViewController = self.rootVC;
and in viewdidload of my RootViewController I wrote:
[self.navVC.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self.navVC.navigationController.navigationBar setTranslucent:YES];
self.navVC.navigationController.navigationBarHidden = NO;
[app.rootVC.view addSubview:self.navVC.view];
self.navVC.delegate = self;
Here, navigationbar is displaying but I am unable to display uibarbutton items on navigation bar. Example code:
leftBtn = [[UIBarButtonItem alloc] initWithTitle:#"Left" style:UIBar
flxBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBa
rightBtn1 = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
rightBtn = [[UIBarButtonItem alloc] initWithTitle:#"Right" style:UIBar
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
Can somebody help me out to resolve my problem. How can I change appearance of navigationbar and how the uibarbutton items can be displayed ?
You will simply need to change your
[self.navVC.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
to
[self.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn,flxBtn]];
and
[self.navVC.navigationItem setLeftBarButtonItem:leftBtn];
to
[self.navigationItem setLeftBarButtonItem:leftBtn];`

How to add a 'cancel' button to a QuickDialog controller?

I am using the regular QuickDialog controller code from their tutorial:
QRootElement *root = [[QRootElement alloc] init];
root.title = #"Hello"
root.grouped = YES;
QSection *section = [[QSection alloc] init];
QEntryElement *hello = [[QEntryElement alloc] initWithTitle:#"Hello World" Value:#""];
[root addSection:section];
[info addElement:hello];
UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];
How can I add a 'cancel' button to the navigation bar? I tried:
navigation.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];
... but that didn't work. Any suggestions?
The proper way to do this is to adjust the code for the `leftBarButtonItem' like this:
navigation.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController.modalViewController dismissModalViewControllerAnimated:YES];
}];
I know this is late but hopefully this will help someone else. I had a similar problem, and was able to use the following code to create a button to remove the form from view
navigation.navigationBar.topItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStyleDone target:self action:#selector(dismissModalViewControllerAnimated:)];
I tried your code in my sample logic and achieved the cancel button in the following way.
You need to set your navigationItem's leftbarbuttonitem on the viewcontroller of a navigation controller, instead of setting straight to your navigationcontroller.
// -- present your root controller.
UINavigationController *navigation = [QuickDialogController controllerWithNavigationForRoot:root];
[self presentModalViewController:navigation animated:YES];
[[[navigation topViewController] navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel handler:^(id sender){
[self.navigationController dismissModalViewControllerAnimated:YES];
}]];

Push a UIBarButtonSystemItemAdd to a different view controller

I'm trying to push an add button to a different view controller and I'm not using a storyboard. The button appears but doesn't do anything when clicked. Here's the code I have:
-(void)pushLogin
{
[self.navigationController pushViewController:[[AddListingViewController alloc] init] animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Listings";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:nil
action:#selector(pushLogin)];
self.navigationItem.rightBarButtonItem = addButton;
}
Any suggestions?
Target should be the viewcontroller, not nil
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pushLogin)];

Can't show popover controller with UIBarButtonItem

i use storyboard for developing UI for my project. There are many issues were solved, but this problem killing me. I have added action for UIBarButtonItem :
- (IBAction)pressAddActionButton:(UIBarButtonItem *)sender {
if (_mode == itemSelect) {
LookUpTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"lookupTable"];
vc.key = #"title";
vc.data = [Linesheet MR_findAllSortedBy:#"title" ascending:YES];
vc.lookUpDelegate = self;
self.myPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[self.myPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
} else {
self.mode = itemSelect;
}
}
If i using storyBoard segue for showing popover - all good, but if i do it in runtime a popover does not show. I should manually create UIBarButtonItem.
Thanks for help!!!
Update, code for buttons:
- (void)setupNavigationItems {
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *addItem;
if (_mode == itemSelect) {
addItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressAddActionButton:)];
} else {
addItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self
action:#selector(pressDoneButton:)];
}
[addItem setStyle:UIBarButtonItemStyleBordered];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressActionButton:)];
[action setStyle:UIBarButtonItemStyleBordered];
[toolbar setItems:[NSArray arrayWithObjects:separator, addItem, action, nil] animated:YES];
}
Make sure you're hitting the code as you expect using a breakpoint on the presentPopoverFromBarButtonItem line. Also, make sure that the myPopoverController property is declared strong if you're using arc, as otherwise it will be nil before the presenting line.
Did you check if LookUpTableViewController *vc actually gets a proper instance? If not then check that your storyboard does have #"lookupTable" set as your controller's identifier.
The code is right. Try replacing the sender with self.navigationItem.leftBarButtonItem or self.navigationItem.rightBarButtonItem. The sender may not be what you are expecting.
Also remove the if (_mode == itemSelect) clause for testing I'm not sure why you need to access the ivar _mode ivar directly.

UINavigation bar back button not responding

I have a cameraview from which I'm pushing another view of with TableViewController. I can push the second view from my main view by using navbar controller. But the back button from the second view which is table view does not respond. I can't figure out why.
Thanks in advance.
#WrightCS Thank you for your immediate response.
In my app delegate.
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController :navController];
[self.window makeKeyAndVisible];
return YES;
Right now I'm using a temporary button to push the view.
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
}
There are a few reasons your back button may not work:
You are using a custom UIButton and the frame/bounds are not correct you can use [yourButtonName sizeToFit]
to size it properly.
You are adding a button programmatically and not adding a click event:
[yourButtonName addTarget:self action:#selector(yourButtonNameTapped:) forControlEvents: UIControlEventTouchUpInside];
Don't forget your event.
...
-(void)yourButtonNamedTapped:(id)sender
{
...
}
docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
If you are using a UINavigationController, back will automatically show up when you use [self.navigationController pushViewController:viewControllerName];
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:tableVC animated:YES];
}
I think it will be helpful to you.
- (void)viewDidLoad
{
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidClicked:)];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
-(void)backButtonDidClicked :(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
It should work for you!!

Resources