I use this code to display a one item Toolbar in UIViewController (which is pushed by UINavigationController). The toolbar shows up and immediately disappear:
UIBarButtonItem * logoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Log out" style:UIBarButtonItemStylePlain target:self action:nil];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
[self setToolbarItems:arr animated:NO];
[self.navigationController setToolbarHidden:NO animated:NO];
Do you know what the problem is?
where is this code? Doesn't seem to be anything out of the ordinary here so either has to do with where you are calling it, or something else that happens afterwards (like setting toolbarHidden to YES later or something)
Related
I have a detailview page, and on top of it the title lable appears to be the title of the row i navigated from. I need to just not display anything in the navigationbar but the title appears and i dont know where to change it.
I also have a backbutton however if i use navigation.backbarbuttonItem then the title of the previous page appears on the button. I want the button to just have back on it. I created a button using leftbarbuttonItem but then the button cannot be styled to look like a backbutton (with one side pointed). It appears as just a rectangular button which i dont like.
Could anyone know how to adjust this?
For number 1, you can just set the viewController title on viewWillAppear or viewDidAppear, lets say
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = #"";
}
For number 2, you can use this code to have the "Back" pointy button.
UIViewController *viewController = [UIViewController alloc] init];
// This code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:viewController animated:YES];
I've programmatically created some UINavigationControllers and added them to a UITabBarController. Everything seems to work fine but I wanted to add a cancel button to the navigation controller but it never shows up. I've tried multiple ways but I can't seem to affect the display of the navigation items at all and I've followed multiple examples from here and other sites but nothing happens.
MyTableViewController *mtvc = [[MyTableViewController alloc] init];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque; // this works
[mtvc release];
// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
myNavController.navigationItem.leftBarButtonItem = closeButton; // never shows up
I also tried adding the button this way
[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO]; // also doesn't do anything
I started getting frustrated so I also tried some other things just to see if I could affect anything, but to no avail
myNavController.title = #"test"; // does nothing
I've tried doing it before and after the navigations controllers were added to the UITabBarController and that didn't help.
I've also tried rightBarButtonItem and tried using initWithTitle: instead of initWithBarButtonSystemItem.
Would someone please illuminate me? Clearly, I'm doing this the wrong way.
Try adding the bar buttons in the loadView method of MyTableViewController like the follwing.
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
self.navigationItem.leftBarButtonItem = closeButton;
I guess that should work.
Have you tried setting the button of the current view controller's navigation item like this:
mtvc.navigationItem.leftBarButtonItem = closeButton;
If you need to do that in Swift 3.0, it is simple like that:
let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel,
target: self,
action: #selector({Your-viewController}.{close-method}))
self.navigationItem.leftBarButtonItem = closeButton
Trying to customize my back button in a drilldown navigation controller.
On my one view controller I have an Add button where the code programatically generates a new UIViewController:
- (void)add:(id)sender
{
MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:#"MyAddViewController" bundle:nil];
[self.navigationController pushViewController:addController animated:YES];
[addController release];
}
This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?
Thanks
self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.
This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.
You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:
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.
So, if you were to change:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
To:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I believe you would get the desired effect.
You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:
- (void)dismissMyView {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Quit" style: UIBarButtonItemStyleBordered
target:self action:#selector(dismissMyView)];
}
If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.
my application has a UIViewController subclass which is being managed by a UINavigationController.
In the viewDidLoad of my UIViewController subclass, I was attempting to add a UIBarButtonItem to the toolbar like this:
settingsButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings"
style:UIBarButtonItemStylePlain target:self action:#selector(viewSettings:)];
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
this wasn't working out for me, so after some googling around, I tried this:
[[self navigationItem] setRightBarButtonItem:settingsButton];
which worked out fine. from reading the UIViewController documentation, I'm still confused about why setToolbarItems wasn't working. I verified in the debugger that the button was in the toolbarItems array in the viewDidAppear method. the button itself just wasn't appearing on my toolbar.
so, my question is, why didn't setToolbarItems work for me in the first code snippet?
I don't have the toolbar configured in my xib for this view controller at all, if that makes a difference.
Yes that make the difference.Whenever you see a bar on view by default for navigation based apps that is not a toolBar actually that is , navigation bar.so you can add item by referencing self.navigationItem.
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]]; essentially populates the navigation controller's bottom toolbar - not the Left and Right top bar buttons.
The bottom toolbar is, by default, not displayed. To display it you must call [self.navigationController setToolbarHidden:NO]
Below is the relevant documentation - UINavigationController Class Reference
toolbar:
The custom toolbar associated with the navigation controller.
(read-only)
#property(nonatomic,readonly) UIToolbar *toolbar Discussion This
property contains a reference to the built-in toolbar managed by the
navigation controller. Access to this toolbar is provided solely for
clients that want to present an action sheet from the toolbar. You
should not modify the UIToolbar object directly.
Management of this toolbar’s contents is done through the custom view
controllers associated with this navigation controller. For each view
controller on the navigation stack, you can assign a custom set of
toolbar items using the setToolbarItems:animated: method of
UIViewController.
The visibility of this toolbar is controlled by the toolbarHidden
property. The toolbar also obeys the hidesBottomBarWhenPushed property
of the currently visible view controller and hides and shows itself
automatically as needed.
try to use
[toolbar setItems:[NSArray arrayWithObject:settingsButton] animated:YES];
instad of :
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];
shani
On ipad apps, you've got to set toolbar items to the "topViewController" (yes this is counter-intuitive).
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:catView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[nav setToolbarHidden:NO animated:YES];
// WRONG: [nav setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
// CORRECT (for ipad apps):
[nav.topViewController setToolbarItems:[NSArray arrayWithObjects:item1, item2, nil] animated:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:NULL];
rButton.action = #selector(refreshAction);
self.navigationItem.rightBarButtonItem = rButton;
[rButton release];
The above code works fine to create a button and add it to the navigation bar, but when I click on the button it crashes the app with a EXC_BAD_ACCESS. If I comment out rButton.action = #selector(refreshAction); clicking the button won't do anything but it doesn't crash either.
- (void)refreshAction {
NSLog(#"refreshAction");
}
This code is in a TableViewController's viewDidLoad method which is pushed onto the navigationController stack from the NavigationViewController viewDidLoad method.
I've spent probably 3 hours trying to get this to work, to no avail.
As usual memory management was the culprit. Loading the tableViewController from the navigationController:
NearbyTableViewController *tableController = [[[NearbyTableViewController alloc] initWithNibName:#"NearbyTableViewController" bundle:nil] autorelease];
self.nearbyTableController = tableController;
[self pushViewController:self.nearbyTableController animated:YES];
[tableController release];
releasing an object set to autorelease... must be the most common error in memory management.
Deleting that line [tableController release] solved the problem
Any luck if you specify #selector(refreshAction) when you create the button, i.e.:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshAction)];
Maybe the target doesn't get saved if you don't also specify the action to the initializer.
I'm not exactly 100% sure why your code does not work, but setting the selector directly in the constructor does work:
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshAction)];