I am programatically adding a UINavigationBar to a UIView, and now need to add a UIBarButtonItem to it. I am trying to use the following:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView)];
[header setItems:[NSArray arrayWithObjects:doneButton, nil] animated:NO];
[doneButton release];
My app crashes and I find this in the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem setNavigationBar:]: unrecognized selector sent to instance 0x4b75c00'
Would appreciate it if someone could please point out what am I doing incorrectly here.
Thanks.
Ricky.
UINavigationBar accepts an array of UINavigationItem objects, each of which contain properties about a given level of the navigation hierarchy. You probably want to create a new UINavigationItem and then set its rightBarButtonItem property to your Done button.
It's unlikely you need to create a new UINavigationItem as the answer currently states. In contrast if you already have a UINavigationBar initialized from a nib which also contains a view, you can just add your UINavigationItem to the topItem property of your UINavigationBar. Something like this:
UIBarButtonItem *closeBtn = [[UIBarButtonItem alloc] initWithTitle:#"Close"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(closeBtnPressed)];
self.navigationBar.topItem.leftBarButtonItem = closeBtn;
[closeBtn release];
Related
I'm new to the Cocoa topic. I build with the Storyboard a small application which runs just fine.
I'm pushing UITableViewController to another one, controlled by the UINavigationViewController. Now I need a UIToolbar with a UIBarButtonItem fixed at the bottom of the screen.
Here can you see my Storyboard So I added via drag and drop a Toolbar and in the toolbar an button into the Period TableViewController, thus it is on the same hierarchic level like the UITableView.
Unfortunately neither the "PayOff" Button nor a bottom Toolbar does appear in the app.
Can you help me, what is wrong with my Storyboard?
In your ViewController try adding this
[self.navigationController setToolbarHidden:NO animated:YES];
and use this to add BarItems to it
[self setToolbarItems:#[item1, item2, item3] animated:YES];
I'm using this method to add a scan button to a UITableViewController:
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
...
...
UIBarButtonItem *scanItem = [[UIBarButtonItem alloc] initWithCustomView:scanButton];
[self setToolbarItems:#[leftSpace, scanItem, rightSpace] animated:YES];
You should be careful because the ToolBar visibility is set for the entire NavigationController you are using and you should show/hide it when needed.
Also the items on it need to be set on each controller (I have this issue, maybe there is a better way to do it)
Hope this helps.
I'm trying to write a tabbed application where each tab is a Navigation Controller. This tab bar appears at every view in the navigation controller as its being inferred on each view.
I would like to replace this tab bar on a detail view with a tool bar with a couple of buttons on it.
I've tried the following code in that detail view's viewDidLoad: method
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:#"Accept"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *decline = [[UIBarButtonItem alloc] initWithTitle:#"Decline"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSArray *items = [NSArray arrayWithObjects:accept, decline, nil];
[self.navigationController.toolbar setItems:items animated:YES];
// code suggested
[self.view addSubview:self.navigationController.toolbar];
It still doesn't show up. Though hides the tab bar now for adding the following line in the view that's presenting the detail view:-
theDetailTableViewController.hidesBottomBarWhenPushed = YES;
Have I missed something?
I usually put toolbarHidden = YES or NO, as applicable, in the viewWillAppear or viewDidAppear methods. I am not sure if that is why it is not working for you, but you need to address when you return to the presenting view anyway.
If you don't address it, the toolbar will still be visible when you go back.
Wherever you are pushing your detailViewController from, do this to hide the Tab Bar in the detail view:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController detailViewController animated:YES];
and in your detail view, just add the ToolBar as a subview to the detailView.
Sup guys,
I have a tabBarController with a navigationController on each tab.
I wanted to set de default right bar button of navigation bar so I wouldn't have to write the same code on 3 different view controllers.
I tried [[UINavigationBar appearance] setRightBarButtonItem:myButton];
but had no success, is says:
-[_UIAppearance setRightBarButtonItem:]: unrecognized selector sent to instance
Then I tried to create my own UINavigationController subclass so I could set the button like:
self.navigationItem.rightBarButtonItem = myButton but again no success, nothing seems to happen probably because I dont have a navigationItem at this time.
Anyone have another solution?
If you inherit from the UIViewController you can set the right button like this
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"foobar.png"] style:UIBarButtonItemStylePlain target:self action:#selector(myAction)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
I am having one problem with UIBarButtons in xCode iOS 4 with Objective-C.
I am following several examples and the error says that the addButtonPressed method was not defined - even though I have the function created before hand like this:
- (void)addButtonPressed
{
NSLog(#"Addbutton pressed", #"");
}
It is also defined in the .h file. Here's my code:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:addButtonPressed];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
Here's the error:
'addButtonPressed' undeclared (first use in this function)
Am I doing something wrong?
Thanks for the help,
Christian Stewart
You should pass a selector for the action argument instead of the method name.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed)];
Is it possible to show toolbar items in a UIViewController inside a popover? I'm doing this in the viewDidLoad method of my view controller:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObject:addButton]];
[addButton release];
Then I'm wrapping this view controller in a UINavigationController (which has a toolbar property, and according to the docs, I'm supposed to use the setToolbarItems method of UIViewController to add items to the toolbar), then presenting it in a popover.
I do not see the toolbar. Are toolbars unsupported when using a popover?
Thanks
Figured it out, apparently the toolbar is hidden by default so you have to do this:
[self.navigationController setToolbarHidden:NO animated:NO];
To make it appear.