So I have a root view with table view. I display the toolbar like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.toolbarHidden = NO;
}
And I implement the setToolbarItems method:
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style:UIBarButtonItemStyleDone target:self action:#selector(goBack:)];
self.navigationController.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
}
The result is an empty tolbar. Why?
From the docs:
toolbarItems
The toolbar items associated with the view controller.
#property(nonatomic, retain) NSArray *toolbarItems
Discussion
This property contains an array of UIBarButtonItem objects and works in >conjunction with a UINavigationController object. If this view controller is >embedded inside a navigation controller interface, and the navigation >controller displays a toolbar, this property identifies the items to display in >that toolbar.
You can set the value of this property explicitly or use the >setToolbarItems:animated: method to animate changes to the visible set of >toolbar items.
In other words, try accessing it through the actual view controller, not it's navigation controller like so:
self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
But who calls your implementation of setToolbarItems?
You're supposed to call setToolbarItems on your own view, not re-implement it. Then, the NavigationController will find them in the instance variable and render them.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *buttonItem;
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style:UIBarButtonItemStyleDone target:self action:#selector(goBack:)];
[self setToolbarItems: [ NSArray arrayWithObject: buttonItem ]];
}
Related
I have the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath(NSIndexPath*)indexPath
{
audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];
[self.navigationController pushViewController:audiChassisInputViewController animated:YES];
self.navigationController.navigationBarHidden = NO;
UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:#"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:#selector(popViewControllerAnimated:)];
[self.navigationController.navigationBar.topItem setLeftBarButtonItem:retourButton];
[self.navigationController.navigationBar.topItem setTitle:#"Chassis Input"];
[retourButton release];
[audiChassisInputViewController release];
}
and this workes...the new view is showed.
in the new view:
myAudiChassisInputViewController.h
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
chassisInputTextView.layer.cornerRadius = 15;
chassisInputTextView.clipsToBounds = YES;
[chassisInputTextView becomeFirstResponder];
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(chassisOkPressed)];
[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
[okButton release];
}
I have no error, but there is no right bar button shown.Anyone, any idea why?
Change this line:
[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
with this line:
[[self navigationItem] setRightBarButtonItem:okButton];
The thing is, by the time viewDidLoad is executed, the top item of the navigation bar (self.navigationController.navigationBar.topItem) is still pointing to the navigation item of the back view controller.
The back view controller is the one that used to be the top view controller before the current top view controller was pushed onto the stack ([[viewControllers objectAtIndex:[viewControllers count] - 2] navigationItem]). The following snippet shows how the top item of the navigation bar is still pointing to the navigation item of the back view controller in viewDidLoad and it is for illustration purposes only:
// the view controllers currently on the navigation stack
NSArray *viewControllers = self.navigationController.viewControllers;
// The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
UIViewController *backViewController = [viewControllers objectAtIndex:[viewControllers count] - 2];
// get the navigation item of the back view controller
UINavigationItem *backNavigationItem = backViewController.navigationItem;
UINavigationItem *topItem = self.navigationController.navigationBar.topItem;
if (backNavigationItem == topItem) {
NSLog(#"This gets logged to the console");
}
Go to your
myAudiChassisInputViewController.m file
place following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:#"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:#selector(popViewControllerAnimated:)];
UIBarButtonItem *itemOkey=[[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(chassisOkPressed)];
self.navigationItem.rightBarButtonItem=itemOkey;
self.navigationItem.leftBarButtonItem=retourButton;
}
I have the valid output as follows that you want to have
Hope it helps to you.
If you have xib file of your class, then add the navigation controller and add the navigation bar and under that add the UIBarButton.
I want to build Grouped View in Modal View which is destination state from "+" button on the Navigation bar in main table view.
I have written this code:
-(void)viewDidLoad
{
//title
self.title = #"Set";
//addBtn
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(toggleEditing)];
self.navigationItem.rightBarButtonItem = addBtn;
[addBtn release];
}
-(IBAction)toggleEditing
{
}
create a subclass of UITableViewController named MyTableViewController
use presentModalViewController:animated: in your current viewController
-(IBAction)toggleEditing {
MyTableViewController *tableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
[self presentModalViewController:tableViewController animated:YES];
}
And you will need a delegate method that tells the viewController from where you showed the modal viewcontroller that a row was selected.
I have a subclass of QLPreviewController that I'm pushing into a UINavigationController. I am able to show/hide the bottom toolbar using:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.toolbarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.toolbarHidden = YES;
}
But items I'm adding to the toolbar aren't showing up:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *testButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(testButtonClicked:)];
NSArray *myToolbarItems = [NSArray arrayWithObjects:testButtonItem, nil];
self.toolbarItems = myToolbarItems;
[testButtonItem release];
}
Any advice would be very appreciated. Thanks.
Where are you allocating and initializing that view that is giving you the problem?
Are you using a splitViewController on iPad?
From the sound of it an object is sticking around for longer than necessary or when being called back it is not being re allocated and initialized properly before attempting to put it back on the stack, if you are using a splitViewController on iPad it handles it's views differently than a simple UINavigationController on an iPhone.
I'm developing iphone app with UITabBarController as main view. Every ViewController in each tab is UINavigationController which must have a same button in leftBarButtonItem. Can I inherit some class from UINavigationController and override it's -(id) initWithRootViewController:(UIViewController *)rootViewController method to realize this ?
I made something like this. But this code doesn't work;
#implementation MainNavagaionController
-(id) initWithRootViewController:(UIViewController *)rootViewController {
if (self = [super initWithRootViewController:rootViewController]) {
// Set user name title
UIBarButtonItem * userNameButton = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStylePlain
target:self
action:nil];
self.navigationItem.leftBarButtonItem = userNameButton;
[userNameButton release];
}
return self;
}
#end
What is shown in the navigation bar is the navigationItem of the current viewcontroller in each navigation controller. You are one level too high when you are setting this left bar button.
I have an UITableViewController which I would like to add UIToolbar to with one button. In the
- (void)viewDidLoad;
method of UITableViewController I have:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
Unfortunately I don't see the toolbar when I run my app.
Any hints? Should I do something more?
The navigationItem property of a view controller is useless if that controller is not displayed inside a UINavigationController.
If your view controller is inside a navigation controller I don't know what the problem is.
Otherwise you can use an UINavigationItem but you need to create a UINavigationBar yourself.
Either in the Interface Builder (add a UINavigationBar and add a UINavigationItem, then connect the UINavigationItem to a property outlet declared your view controller (you don't need to connect the Bar).
Or in your code :
UIBarButtonItem *item = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(pressButton1:)];
UINavigationItem* navItem = [[UINavigationItem alloc] init];
navItem.rightBarButtonItem = item;
navItem.title = #"Your title";
naviBar = [[UINavigationBar alloc] init];
naviBar.items = [NSArray arrayWithObject:navItem];
naviBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0);
[self.view addSubview:naviBar];
[navItem release];
Your method requires an autorelease:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(pressButton1:)] autorelease];
self.navigationItem.title = #"Some title";
self.navigationItem.leftBarButtonItem = button;
}
There's nothing wrong with your code per se. Your question states you want to add an UIToolBar to your view? Really? Or do you just want to add a button to the NavigationItem for UITableView?
If you don't have to use a UITableViewController and are not using a UINavigationController in your app already, you can set your view controller up as a regular UIViewController with a toolbar and tableview.
To do this in IB, drag out a UIViewController object and add a toolbar and tableview. Hook up outlets for both and set the delegate and datasource of the tableview to Files Owner. Add any other toolbar items or buttons and give them outlets and methods if you need them for buttons, etc. In your ViewController.h file, make sure you sign it up to conform to the UITableViewDataSource and UITabBarDelegate:
#interface ViewController : UIViewController <UITableViewDataSource, UITabBarDelegate>
From there, just build out your tableview delegate and datasource methods like you normally would, and write your button action methods for any buttons you added to the toolbar.
You just didn't show the toolbar. It is hidden by default. To fix it, you just put this line of code:
self.navigationController.toolbarHidden = NO;
I tried it and it worked. Just make sure that you put in the implementation file's viewDidLoad method.