Resetting UISearchbar programmatically - iphone

Is there a possibility to reset a UISearchbar with searchDisplayController programmatically which is active and already contains text?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResultsTableView) {
searchDisplayController.searchResultsTableView.hidden = YES;
searchBar.showsCancelButton = NO;
[searchBar resignFirstResponder];
}
}
This solution is working, but there is still text in the searchBar. If i add those lines of code:
searchBar.text = #"";
There is always a black transparent view left.
Any solutions?

Here ya go. This is a delegate method called when the user hits cancel. If you want to wire it up differently just declare your search bar as an outlet and reference it. Anyway:
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
searchBar.text = #"";
[searchBar resignFirstResponder];
}

Pretty sure what you're looking for is UISearchDisplayController.active
From the SDK:
#property(nonatomic, getter=isActive) BOOL active
If you set this value directly, any change is performed without animation. Use setActive:animated: if a change in state should be animated.
When the user focus in the search field of a managed search bar, the search display controller automatically displays the search interface. You can use this property to force the search interface to appear.

This thread is so old it has dust. Still here we are in swift so
<#your UISearchController variable#>.isActive = false

don't you have to retire the firstResponder

[self.searchBar resignFirstResponder]

You can also explicitly hide the resultsTableView, if that's what you want:
searchDC.searchResultsTableView.hidden=YES;
(searchDC is an instance of UISearchDisplayController)
This will help you with hiding the Cancel button and stuff: http://www.alexandre-gomes.com/?p=418

Related

How to remove the edit button on a Master-Detail Template

I made a project in interface builder with the Master-Detail Template and I'd like to get rid of the edit button.
I wrote (in MasterViewController):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}
This disables the edit button, however the button is still there.
Then I tried (in the viewDidLoad, after connecting the tableView property to my MasterViewController class):
[self.tableView setEditing:NO];
However, the button is still there.
You need to remove the button altogether. setEditing has to do with wether or not the tableview is in edit mode or not, so try:
self.navigationItem.rightBarButtonItem = nil;
In your viewDidLoad method.
Also make sure you don't see a line of code in viewDidLoad that looks like:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
If yes, delete or comment it out.

Using UITextField with a UISearchDisplayController in place of a UISearchBar

How can I use a textfield rather than a search bar with the UISearchDisplay controller?
I want to completely customize the search bar by getting rid of the magnifying glass icon and customizing the background. Also stopping it from resizing and bringing up the 'cancel' button. I see some people using hacky ways to do this by editing parts of the search bar api that weren't supposed to be edited. So it seems the more accepted way to do customization on this level would be to use a UITextfield instead of a UISearchBar. But there doesn't seem to be ANY info on the web about doing this!
If I use a textfield, what methods do I need to call when text changes to make the UISearchDisplayController work?
The trick here is to rewrite the UISearchDisplayController. It really only does 3 things.
Move the searchbar up to the top of the view and hide the UINavigationBar.
Place a transparent cover view over the remainder of your view.
Show a UITableView with any search results.
So start by registering your UIViewController as a delegate for the UITextField and..
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//here is where you open the search.
//animate your textfield to y = 0.
//I usually make the search tableview and the cover a separate view,
//so I add them to my view here.
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *searchText = [[NSString alloc] initWithString:[textField.text stringByReplacingCharactersInRange:range withString:string]];
//display your search results in your table here.
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//hide all of your search stuff
[self.navigationController setNavigationBarHidden:NO animated:YES];
}

exit edit mode and update nav bar items

With reference to this question: Exit Edit Mode
about exiting edit mode when the last row is being deleted, my question is - how do you update the navigation bar "edit" item? After deleting the last row, I'd like to remove this nav bar item altogether AND exit edit mode (which is done per the question below) AND revert this button status back to "Edit" (rather than "Done" which is its status after deleting the last row).
Thats what I am doing now:
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
...
if ([section count] == 0) { //last row in the section
[listOfItems removeObject:accessNumbers]; //updating my data source
tblSimpleTable.editing = NO; //added per the question above
// self.navigationItem.rightBarButtonItem = nil; --> thats what ideally i would want to do
// [self setEditing:YES animated:YES]; --> adding this manually doen't help
}
else
{
...
}
}
}
Thank you for the help!
UPDATE: adding this line doesn't help. I stil need to click on the nav bar item "Done" to exit the editing mode.
[self.tblSimpleTable setEditing:YES animated:YES];
if I also hide the nav bar item, I essentially can't exit the edit mode at all, and the screen is frozen (I have some other buttons on the view that simply dont react to touch anymore in that case).
According to the apple documentation, you cannot call [self.tableview setEditing:NO animated:YES] from within your tableView:commitEditingStyle:forRowAtIndexPath:. Here is the relevant excerpt:
Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.
Presumably one could then create a selector that turns off editing mode and removes the button.
Can you enforce this rule in another callback?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL answer = [section count] > 1;
self.navigationItem.rightBarButtonItem = (answer)? self.editButtonItem : nil;
return answer;
}
I was having issues doing the same thing (not able to get the status of the barButtonItem to revert to 'Edit' after calling setEditing) and learned from this answer https://stackoverflow.com/a/11490594/2888978 that the way to get change the 'Edit' back to 'Done' on the nave bar is to call setEditing on the view controller, not the table. Then you can set the barButtonItem to .None to remove it from the nav bar when the table is empty.
So instead of calling:
self.tableView.setEditing(false, animated: true)
You would call:
self.setEditing(false, animated: true)
Otherwise only the editing mode of the cells will change.
To leave editing mode, use this:
[self.tableview setEditing:NO animated:YES];
To remove the button all-together, use:
// Note that this only removes the right-most button. If you want to remove all of the buttons on the right side, use rightBarButtonItems instead.
self.navigationItem.rightBarButtonItem = nil;
// If you want it animated, use:
[self.navigationItem setRightBarButtonItem:nil animated:YES];

resignFirstResponder for UITextField with search returnkey

Simple question: how can I resign my textfield if my "done" key is Search. Essentially, what do I do if the user does not want to search, but instead wants to cancel...
thanks
You can use: a cancel button for SearchBar and need to implement this SearchBar delegate :
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
isSearching = NO; //This is a flag which specifies if searching is going on or not.
searchBar.text = #""; //Clears out search bar text
[self resetSearch]; //Reset search resets all the flags and variables.
[self.leadsTable reloadData]; //Reloads the tableView
[searchBar resignFirstResponder]; //Resigns responder from Search bar
}
This is a proper way to resign the responder if user doesn't want to search.
Look at how you can add an in-built Cancel button in UISearchBar. Check the property "Shows Cancel Button" (Red Arrow highlight)
EDIT:
STEP-1:
Check conditionally whether your textField's text is blank? If so resignFirstResponder for the TextField. You need to set the Delegate to self for the UITextField using code:
txtField.delegate = self;
Or you can do the same from the interface builder by connecting TextField's delegate to File's Owner.
STEP-2: You need to implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField.text isEqualToString:#""])
{
[textField resignFirstResponder];
}
return YES;
}
EDIT-1:
Now create a UIToolbar with one bar button labeled 'Cancel'
Once you have done that:
You can write an button click event for UIToolBar:
-(IBAction)cancelClicked:(id)sender
{
[txtField resignFirstResponder];
}
Once you have done that you can now just write:
txtField.inputAccessoryView = toolbarOutlet;
Hope this helps you.
The text on the return button is irrelevant to the discussion. You want to know how to resign first responder without pressing the return button, and there are a few ways to do it.
Use the inputAccessoryView of the text field to display a separate cancel button in a toolbar above the keyboard.
Use a tap gesture recognizer on the field's superview to recognize when the user taps outside the field, and call [self.view endEditing:YES] (where self is your view controller). This will cause the first responder to resign. (This is very finicky in a scroll view.)
Swap out the rightBarButtonItem of the current view controller for a cancel bar button item while editing, assuming you have a UINavigationBar on screen at the time. When editing ends, swap back in the regular right bar button item, if any.

Designing iOS SearchBar

I want to have a simple SearchBar in ObjectiveC. Using UISearchBar or UISearchBarDelegate is confusing me. I could have used a UITextField but it does not have the look & feel of a search bar.
As in the image attached, I want just the searchbar no UITableView associated with it. The image has a TableView attached but you get the point. Also after someone enters text into the searchBar & pressed "enter" how do I retrieve the text?
UPDATE: I am aware of these links which discuss the same, but they are more in light with using tables.
http://blog.webscale.co.in/?p=228
http://ved-dimensions.blogspot.com/2009/02/iphone-development-adding-search-bar-in.html
How to implement search bar in iPhone?
UISearchBar Sample Code
UISearchBar in UITableViewController?
Just make your view controller implement the UISearchBarDelegate. In your xib file, all you need to do is to add a UISearchBar to your view and configure it as necessary, create an outlet for it (optional really but helps to be explicit), and assign the delegate outlet to your view controller.
Then, to respond to the search bar events, implement the UISearchBarDelegate protocol methods as necessary. For example:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self handleSearch:searchBar];
}
- (void)handleSearch:(UISearchBar *)searchBar {
NSLog(#"User searched for %#", searchBar.text);
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
NSLog(#"User canceled search");
[searchBar resignFirstResponder]; // if you want the keyboard to go away
}