UISearchBar in UINavigationController with cancel button not selectable - objective-c

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.

Related

NSButtons Inside NSPopover View Controller

I've developed for Mac before but this is the first time I've attempted to use the NSPopover control, which seemed like a great idea to start out with but so far is causing me no end of problems. The applciation is a menu bar application. I have two NSButton objects in the NSPopover's view controller, the NSPopover is being created programmatically in another subclass of NSButton, the same button which it is being shown relative to. This NSButton that it is being shown relative to is contained along with some other buttons in an NSMenuItem
The popup, containing the two buttons, is being shown fine (see screenshot below), however, despite the 'Yes' button being highlighted with a focus ring, neither button responds to click events, they do not even graphically click in like I would expect them to.
And this is the code that creates the NSPopover and positions it onscreen:
someViewController *confirmationDialogue = [[someViewController alloc] initWithNibName:#"someViewController" bundle:nil];
popOver = [[NSPopover alloc] init];
[popOver setBehavior:NSPopoverAppearanceMinimal];
[popOver setBehavior:NSPopoverBehaviorTransient];
[popOver setContentViewController:confirmationDialogue];
[popOver showRelativeToRect:NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height))
ofView:self
preferredEdge:NSMaxYEdge];
Has anyone got any kind of solution/workaround to this?
Thanks in advance :)
P.s. This is my first question on SO, so I hope I've provided enough information but I'll give any more details as needed.

How to remove the back arrow ios 7 navigation bar?

In my custom navigation bar, i want to use hide default back button and use my customize left button, but even if the back button is hidden, it sill take some space at the left end of navigation bar.
How to get rid of back arrow button completely?
I have tried to set backBarButtonItem and 'hidesBackButtonproperties ofUINavigationItem`, but none of these works.
UPDATED:
- (void) viewDidLoad{
//...
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_backButtonView];
//...
}
I use Spark Inspector to inspect the view hierarchy.
As you see, the back arrow button still takes some space in navigation bar.
UPDATED:
The back arrow is actually an instance of _UINavigationBarBackIndicatorView, it look like a private class.
UPDATED:
It turns out the problem have nothing to do with the UINavigationbarBackIndicatorView. iOS 7 will add spacing before the left item.
this problem is similar to this. Found the answer.
You should set hidesBackButton to YES before you push the controller. That is the navigation controller should be set hidebackbutton in previous viewcontroller if the pushing view must have hidden back button.
I hope this may help you.
It turns out my problem is similar to this
The solution from that question work for me.

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

What's going on with my UIButtons in a xib? [duplicate]

I have a set of UIButtons (defined in a xib) who have labels that need to be updated periodically. In the ViewDidLoad method of the view controller of those buttons' superview, I have an update method that does, for each button:
button.titleLabel.text = #"Relevant Text";
[button setNeedsDisplay];
and when you tap a button, another method runs which pops up a UIAlertView, which in turn calls back a method on the view controller which does much the same thing as the initial text setting method:
button.titleLabel.text = #"New Text";
[button setNeedsDisplay];
however, this code simply isn't working, the button label's text doesn't get updated in either method, it remains a blank white button. In the xib I don't define any text on the buttons - there's no point, the button text doesn't make sense unless it's set at runtime. Anyway, on a lark, I decided to set the text of one of the buttons to "test test test".
Now, when I tap that particular button, it pops up the UIAlertView but in the background changes the text of the button to "test test test test". And this time, the UIAlertView callback does what I expect it to and sets the text for only that button. When I hit it again, the text goes back to "test test test test" until I dismiss the UIAlertView, which again will run the callback method and set the button text to whatever the method should.
I have no idea what's going on here, or why setting the text initially in the xib has any relation to whether or not I can set that text later programatically. Obviously this isn't the behavior I want, I want to know how to for sure set the text on the buttons.
Edit: SVD's advice about setTitle:ForState: solved my problem, thanks. I'm still curious though as to why the title label set in the .xib shows up, but only when I have a UIAlertView pop up.
You may need to use [setTitle: forState:] to set the button title for normal and highlighted (or selected) state.
(And do make sure the button is connected to the outlet, as jtbandes points out).

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.