How to add UIBarButton Programmatically to navBar of UIViewController - ios

I want to add a UIBarButton to the "navBar" of the viewController.
Here's my code:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"+ Contact"
style:UIBarButtonItemStylePlain
target:self
action:#selector(showPicker:)];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
Nothing shows up on the "navBar". I also tried adding it through the storyboard but I couldn't connect it to the interface of my viewController as an IBAction.
Can't figure it out.

Related

Using the same barButtonItems and toolBarItems for all view controllers

I've created my toolBar and navigationBar and added the respective items to it. I'm still new to coding but its my understanding that UINavigationController only displays the navBar and toolBar for the viewController in the top of the seque. I'm doing this without storyboards and in swift.
If you want to add more than one bar button items to UINavigationBar programatically, you can do that like
UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil]];
Hope this will help you.

Objective-C add UIBarButtonItem to UIViewController

I am trying to add an UIBarButtonItem and an UIViewController with the following code:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"back", #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
But when I run my app, the UIBarButtonItem does not appear. Please Help.
Before you localize your string, make sure it works with a regular string. So then you know if the code for the button is incorrect or the if you didn't set up your localized string properly.
You need to embed your view controller into navigation controller at that time so can find the UIBarButtonItem. Because it is navigation item you need to embed inside the navigation controller.

My Custom UINavigationController won't show right button

I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;

rightbarbutton disappear on tabbar n storyboard

I'm using storyboard on xcode5.
and I want to display put right bar button on navigation within tabbar.
structure is tabbar controller -> navigation controller -> view controller
I tried following code on view controller.
But, Nothing is displayed.
- (void)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"btn"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.rightBarButtonItem = addButton;
}
Does someone know what's wrong?
In your ViewDidLoad
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"More" style:UIBarButtonItemStyleBordered target:self action:#selector(toggleMenu:)];
or Storyboard

UIBarButtonItem segue to next viewcontroller

I created a UIBarButtonItem programmatically and want it to go to the next ViewController when it is pushed.
This is how I create the custom UIBarButtonItem in the viewDidLoad:
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:#"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(foundView)];
After that I created this method:
-(void)foundView:(id)sender {
UIViewController *foundVC = [self.storyboard instantiateViewControllerWithIdentifier:#"foundView"];
[self.navigationController pushViewController:foundVC animated:YES];
}
The ViewController where I want to go after the UIBarButtonItem is clicked has a Storyboard ID: "foundView".
What am I doing wrong here?
Change your code like this :
UIBarButtonItem *foundButton = [[UIBarButtonItem alloc]
initWithTitle:#"Found it!"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(foundView:)];
you forgot : after foundView in your selector so you don't call the method.
You can create a segue from ViewControllerA to ViewControllerB. Note, this segue connects two view controllers. Not a button to a view controller. Now, on the storyboard, give the segue a descriptive name. For example: segueFoundView.
Now, do this:
-(void)foundView:(id)sender {
[self performSegueWithIdentifier:#"segueFoundView" sender:self];
}
Edit: corrected spelling

Resources