Adding BarButtonItems to backButton in view controller - objective-c

i have a view controller, which is standalone and has two left UIBarButtonItem, however when i push it, i want to have these two buttons + the back button
i tried
- (void)viewDidLoad
{
[super viewDidLoad];
// back
if (self.navigationController.navigationItem.backBarButtonItem) {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.navigationController.navigationItem.backBarButtonItem, self.barButtonFilter, self.barButtonFilterContacts, nil];
} else {
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:self.barButtonFilter, self.barButtonFilterContacts, nil];
}
}
if there is a back button, than add, else replace
but i didnt work

I am not able to get your problem but according to your caption you want to replace you back button of UINavigationController with a bar button item, in that case you simply need to have a custom button in place of back Button:
UIBarButtonItem *backButton= [[UIBarButtonItem alloc] initWithTitle:#"yourTitle" style:UIBarButtonItemStylePlain target:self action:#selector(someFunction:)];
self.navigationItem.rightBarButtonItem = backButton;
[backButtonrelease];
If this is not your problem please elaborate.

So the issue here is that a UINavigationBar can only have one leftButtonItem and one rightButtonItem. But What you can do is in the center of the UINavigationBar you can have a UIView. You can use this to place the buttons on.
Someone has the code here: adding-buttons-to-the-titleview-of-navigationbar-without-having-to-repeat-code

From Apple's iOS Human Interface Guidelines:
Use a toolbar instead of a navigation bar if you need to offer a
larger set of controls, or you do not need to enable navigation.
Avoid crowding a navigation bar with additional controls, even if
there appears to be enough space. The navigation bar should contain no
more than a view’s current title, the back button, and one control
that manages the view’s contents. If, instead, you use a segmented
control in the navigation bar, the bar should not display a title and
it should not contain any controls other than the segmented control.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html

Related

How to specify the left, right, or title View button in XIB

I do not want to create a custom navigation bar.
I am pushing a UIViewController and I want to customize how the navigation bar looks for that UIViewController
In story board, we just specify the segue and a nav bar show up on the screen. We just drag and drop UIBarItem to the left and right.
In XIB, the navigationBar simply doesn't show up.
I can add navigation Item but the one I added is ignored.
I've heard that there used to be an outlet called navigationItem but it's deprecated for reason I do not know.
I can add UINavigationBar, however that would be adding my own custom bar. I want the navBar that's provided by UINavigationController.
The appearance of the Navigation Bar in the storyboard designer is just there to illustrate how your screen will look when you load your ViewController inside a UINavigationController. It doesn't mean that you actually have a navigation controller in your app.
You need to add a UINavigationController to your storyboard (probably as the first scene), and then connect your ViewController to it (as the root view controller).
Then you should be able to set your title in the storyboard designer, and drag bar button items onto the navigation bar.
See also the answer to this question.
You can setup it in code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Title";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back"style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItem = "Create button" ;
self.navigationItem.leftBarButtonItem = "Create button" ;
self.navigationItem.titleView = "Create custom title view";
}
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

How to put a tableView's Edit button in the toolbar when a search bar is shown (as in Apple's mail app)?

I would like to put the Edit button in the toolbar at the bottom. The Edit button is usually instantiated with self.navigationItem.rightBarButtonItem = self.editButtonItem in viewDidLoad and normally placed in the navigation bar.
When trying to put the Edit button (= self.editButtonItem) in the toolbar (by adding it to the toolbar items), the buttom does not appear. However, all other toolbar items, which I have added using Interface Builder are presented correctly.
How would you recommend adding this button to the toolbar?
The reason for adding the Edit button to the toolbar is to be enable the table's edit mode when a UISearchBar enabled search is currently active and the searchbar hides the edit button in the navigation bar.
I would like to get the same behavior as in Apple's email app, where the Edit button is shown in the toolbar when search is active.
I would appreciate any suggestions.
Thank you for your help!!
You can add a Edit button to the toolbar this way:
self.editButton =
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:nil
action:nil]
autorelease];
NSArray *toolbarItems =
[NSArray arrayWithObjects:self.editButton,nil];
self.toolbar.items = toolbarItems;
If you want to hide/show it, just toggle its hidden property:
self.editButton.hidden = YES;
You could do it manually, by creating a new UIBarButtonItem with style UIBarButtonSystemItemEdit. Then simply add it to the tabbar, assign a target and action and call [tableView setEditing:YES animated:YES];.

Can't rename or set new backBarButtonItem in UINavigationBar

I am using the XCode 4.3.2 and only want to set the text of my back button in my UINavigationBar.
Theh simple way is use
- (void)loadView {
[super loadView];
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style: UIBarButtonItemStyleBordered
target:nil
action:nil];
}
But id don't work. I open my Storyboard and see this:
The back button is disable. So, I can't set the button or disable it. What can I do now?
The backBarButton is used to set what the back button will say on your next view controller that you push, i.e. what the back button will say to let the user know how to get back to the current view controller. It's not used to control what the back button says on the current view controller. That's a common source of confusion.
In terms of what appears in IB, I'm not at all sure about what you're doing there. If you have a navigation bar, and you're pushing view controllers as the user goes through the app, there's nothing you need to do in IB (other than making sure you've got your navigation controller set up in your initial view). But there's no need to set up the back button in IB.
If you're trying to do something else, let us know, but that's the standard flow.

How to use a segmented control in the toolbar to switch views?

As it has been asked many times, I am trying to switch views by using a segmented control correctly. I cannot just hide/display because the views are too complicated.
I have gotten it to work using this solution from this answer, but this places the segmented control in the title bar. I would like to palce it on the bottom, in a toolbar.
I have tried hooking it up via IB and declaring each subviews control, but no luck. I think it has something to do with the #selector section.
Can someone please shed some light on this for me?
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
// if you are in a navigation controller:
[self.navigationController setToolbarHidden:NO];
self.toolbarItems = [NSArray arrayWithObject:item];
// else, created a UIToolBar called toolBar at the bottom of the view and...
toolBar.items = [NSArray arrayWithObject:item];

How do I add a navigation bar's Done button to a popover's passthroughViews?

I'm working in an iPad app that has a split view with a navigation controller in the detail view. The deepest view that can be in the navigation stack is an edit view where the user can edit data. I put an edit button as the rightBarButtonItem and when editing starts, change it to a done button.
When editing commences and the user touches on a particular field, I present a popoverview with a list of possible choices filtered by what they are typing - a form of autofill based on all the values of that field in all other objects.
This works fine, except if you try touching on the done button. The popover eats this touch and dismisses itself. So the user has to touch done again.
I tried using the uipopovercontroller's passthroughViews property, but UIBarButtonItem is not a view and there is no documented way to get the view for the done button or even the navigation bar. I can access the variable in gdb, but it isn't accessible via KVC.
Any ideas on how I can prevent the need to tap done twice?
I've thought about a gesture recognizer on the window, but that seems messy and I'd have to handle rotation.
In case anyone gets here from google, copypaste from other question:
The only solution I found for now is to create UIBarButtonItem with custom UIButton using
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//code for styling button
UIBarButtonItem *b = [[[UIBarButtonItem alloc]
initWithCustomView:button]
autorelease]
and then
popoverController.passthroughViews = [NSArray arrayWithObject:b.customView];
But be prepared - you cannot create UIButton that looks like UIBarButtoItem. I ended up with creating image that reassembled UIBarButtonItem.