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

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];.

Related

xcode navigation button url

I am trying to add a "Home" button to my app, where if you click on a button in the toolbar, if will take you to a specific url "google.com".
So I can put a button in the nav bar, and make it say home.
and I can put this in the viewcontroller.m:
-(void)openGoogleURL
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Is what I put in viewcontroller correct? How do I hook up my button?
UINavigationController uses a UINavigationItem object of each UIViewController to display the items in navigation bar. So you can add the button you want in the bar by doing something like this on your View Controller:
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(openGoogleURL)]];
That way the button target-action will be linked with the function you've defined in your UIViewController.
Note that for every view controller you want to add this button, you'll need this code.
You can wire the button click event to your code either programmatically or by using Interface Builder. Here's some documentation from the Apple's website: https://developer.apple.com/library/mac/#documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html

Adding BarButtonItems to backButton in view controller

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

UISearchBar in UINavigationController with cancel button not selectable

I have added a UISearchBar to the UINavigationController just fine, but as soon as I add the cancel button to the search bar I can no longer select it by touching it.
The code I'm using to create the search bar and add it is:
UISearchBar *theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,0.0f)];
theSearchBar.delegate = self;
[theSearchBar setPlaceholder:#"Search iPad"];
[theSearchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.titleView = theSearchBar;
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[theSearchBar release];
If I remove the line to add the cancel button I'm able to focus on the search bar and the search works correctly. As soon as I put that line in, it displays properly, but I can no longer focus on it.
I've tried putting it into a simple UIView container, but that didn't solve the problem either.
What about adding the cancel button could be causing this to fail?
UPDATE:
I ended up not using the cancel button because I determined it wasn't necessary the way the search bar was being used.
I would definitely recommend a UISearchDisplayController: apple docs.
It has a cancel button built right in, and by placing it in interface builder you can save a lot of code. Less code = less bugs.

Show NSStatusItem menu only if App is active

I'm using this method to show the NSStatusItem menu only if application is Active.
-(void)menuWillOpen:(NSMenu*)menu{
if(![NSApp isActive]){
[menu cancelTracking];
}
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:self];
}
It perfectly works, but cancelTracking seems to block the blue highlight of the NSStatusItem. So when i click on status menu item it doesn't show the submenu and it presents the main window, but the icon is not highlighted.
Is there a way to make it happen ?
I suggest creating a custom view if you need more precise control over when the status item highlights itself. Then you can use mouseDown: etc. This is a good example of putting your custom view in a status 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.