My Custom UINavigationController won't show right button - ios

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;

Related

Easier way to implement navigation bar item with tab bar controller?

I have a tab bar controller as my root view with 5 navigation controllers (one for each tab). The navigation bar in each tab will have a button that has the same functionality across all tabs. Is there an easier way to add this button (and respond to selection) than copy/pasting it into each navigation controller?
EDIT:
In my custom navigation controller's child view controller, I have:
- (void)viewDidLoad
{
[super viewDidLoad];
...
CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeNavigationController"];
[navController addNotificationsButton:YES searchButton:NO];
}
and in the custom navigation controller I have:
-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search { //Choose which bar button items to add
NSMutableArray *barItems = [[NSMutableArray alloc]init];
if (notifications) {
//Create button and add to array
UIImage *notificationsImage = [UIImage imageNamed:#"first"];
UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:#selector(notificationsTap:)];
[barItems addObject:notificationsButton];
}
if (search) {
//Create button and add to array
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchTap:)];
[barItems addObject:searchButton];
}
[self.navigationItem setRightBarButtonItems:barItems];
}
But I just get an empty navigation bar when the child view controller loads. Any ideas?
EDIT 2:
I just had to add an argument for the view controller who's navigation bar I wanted to add buttons to. Here is the final implementation...
for CustomViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
...
CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:#"HomeNavigationController"];
[navController addNotificationsButton:YES searchButton:NO forViewController:self];
}
For CustomNavigationController.m
-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search forViewController:(UIViewController*)vc { //Choose which bar button items to add
NSMutableArray *barItems = [[NSMutableArray alloc]init];
if (notifications) {
//Create button and add to array
UIImage *notificationsImage = [UIImage imageNamed:#"first"];
UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:#selector(notificationsTap:)];
[barItems addObject:notificationsButton];
}
if (search) {
//Create button and add to array
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchTap:)];
[barItems addObject:searchButton];
}
vc.navigationItem.rightBarButtonItems = barItems;
}
You need to create a subclass of UINavigationController,
configure it as you want (so with your button),
and set that as class of your 5 Navigation Controller.
Obviously i can't write here all your apps, but the important is that you have a way.
If you are a developer at the first times, i encourage you to study about subclassing etc.
Subclass navigation bar and add button as part of subclass. Now you can use 5 instances of the subclass instead of the 5 base navigation bars and cut out the repeated work of adding the buttons.

how to fire back button to in the navigationcontroller modal dialog view

First View has a compose button that calls the composeview as modal dialog. I have added a cancel button to the second view. Where do I add the action for the cancel button?
ComposeTweetVC* composeViewController = [[ComposeTweetVC alloc] initWithNibName:#"ComposeTweetVC" bundle:[NSBundle mainBundle]];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:composeViewController];
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(backPressed:)];
composeViewController.navigationItem.leftBarButtonItem = newBackButton;
[self presentViewController:nvc animated:YES completion:nil];
Where do I add the backPressed method? If I add to the viewcontroller calling the composeviewcontroller, it is never called.
You can add it wherever you want.
Your problem is:
target:nil
You are essentially calling [nil backPressed:] which is a NOOP.
Try changing nil to self or composeViewController, or whatever object you want to be notified when the button is pressed.
You are setting the target to nil so the backPressed: will never be called
try to change this line
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(backPressed:)];
to be
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target: composeViewController
action:#selector(backPressed:)];
set your target
initWithTitle:style:target:action:
Initializes a new item using the specified title and other properties.
- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action
Parameters
title
The item’s title. If nil a title is not displayed.
style
The style of the item. One of the constants defined in UIBarButtonItemStyle.
target
The object that receives the action message.
action
The action to send to target when this item is selected.
and then use
-(void) backPressed:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}

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

Button not being added on navigation controller

- (void)viewDidLoad
{
[super viewDidLoad];
ac = [[AddContacts alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:ac];
[self.view addSubview:navigationController.view];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
}
Why is my button not being added on the navigation controller?
The UIBarButtonItems are not controlled by the navigation controller, but by each of the view controllers it contains - each UIViewController can have different buttons. Try:
ac = [[AddContacts alloc]init];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
ac.navigationItem.rightBarButtonItem = anotherButton;
Then initialize the UINavigationController as you have been doing.
There are a few things here that could be causing issues.
Probably what the main issue is this line:
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
I am pretty sure that what you want to be setting is the right bar button item on the ac view controller's navigation item.
ac.navigationItem.rightBarButtonItem = anotherButton
A few other things though:
Don't have two letter variable names. "ac" is very ambiguous, "addContacts" would provide more information, "addContactsViewController" would provide even more
Are you implementing your own navigationController property in a UIViewController subclass? This is not recommended as it is overriding the navigationController property that UIViewController already has. Give it a different name.
Is the -viewDidLoad method on the parent view controller the place to be assigning the right bar button of your AddContacts object? Consider instead putting the code to set the bar button in the implementation of AddContacts instead.

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:#selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#“back”, #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:#selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];

Resources