Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I have a UITextField and I would like to create something a bit like the drop down menu in the ios contact app.
When the user starts typing, I would like the menu to drop down. It doesn't really need to limit the data it shows based on what the names but it would be great if it could (ex: it if the user types "m" it only shows strings beginning with m and so on). If the user selects one, that type would be sent to the UITextField where it would be displayed. If there is any open source picker like this, it would be great. If not, is there a way to present something like that that contains all items from the array. This would not have to be complex, it wouldn't have to limit the data shown when the user types or anything.
Response based on this link. Will show you how to get URL values in a dropdown that will autocomplete.
You need to have an NSMutableArray with the possible autocomplete values. In this example, we’ll use an NSMutableArray of pastURLs, and every time the user browses to a URL we’ll add it to the array.
You need to create a UITable to show the values
autocompleteTableView = [[UITableView alloc] initWithFrame:
CGRectMake(0, 80, 320, 120) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
You need to show the table when the field is being edited
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
And finally only show stuff in the table that is being edited
-(void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
// Put anything that starts with this substring into the autocompleteUrls array
// The items in this array is what will show up in the table view
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.location == 0) {
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
Don't forget to add the proper UITable and UITextfield delegates to your .h file.
Related
I Am using googlemapssdk in my app instead of mapkit,how I have a text field I have to use that textfield for searching places in the google maps.
How to use text for searching places in the googlemaps sdk and how to get the searched place latitude and longitude of that particular place.
I have gone through all the questions in the stack overflow but I can't able to understand them so please help me out to solve this problem.
Thanks for quick Answers.
//You can perform search by tapping search button using the following code
- (IBAction)searchButtonPressed:(id)sender {
[self searchForLocationWithText:self.searchTextField.text];
}
//Else If you want to perform the search on typing each letter, You can use this delegate method
- (void)textFieldDidChange:(NSNotification *)notification {
[self searchForLocationWithText:self.searchTextField.text];
}
//Method for searching
- (void)searchForLocationWithText:(NSString *)searchString{
GMSCoordinateBounds *bounds = [self getGMSBoundsWithRadiusInMeters:10000];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.country=#"IN";
[place_client autocompleteQuery:searchString bounds:bounds filter:filter callback:^(NSArray *results, NSError *error) {
//Process result
}];
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
There are a lot of similar questions and answers, but they don't solve the main problem: how to properly add any text field in cocos2d with its workable delegate.
For example if I create UITextfield and simply add it to [[CCDirector sharedDirector] view] then it will call textFieldShouldReturn: method only. No more methods doesn't work, even textViewDidBeginEditing:
I gave the same result with using of separate view and UITextField. I also tried to create a separate UIViewController to use its view with UITextField but it even make my app falling down
1: Make sure your CCNode contains the UITextFieldDelegate:
#interface MyLayer : CCLayer <UITextFieldDelegate>
2: Init the UITextField as per usual:
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, fieldWidth, fieldHeight)];
nameField.textAlignment = NSTextAlignmentCenter;
nameField.font = [UIFont fontWithName:kFontName size:24];
nameField.text = #"Enter Name";
nameField.delegate = self;
nameField.backgroundColor = [UIColor grayColor];
nameField.textColor = [UIColor whiteColor];
nameField.autocorrectionType = UITextAutocorrectionTypeNo;
3: Add the field to the CCDirector view:
[[[CCDirector sharedDirector] view] addSubview:nameField];
4: Implement the UITextField delegates in your CCNode:
- (BOOL)textFieldShouldReturn:(UITextField*)textField
- (void)textFieldDidBeginEditing:(UITextField *)textField
My mistake was that UITextFieldDelegate and UITextViewDelegate have some similar mehods, and I used some methods from the second delegate. Example:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Of course I wrote (UITextView *)textField at the end.
I'm have a weird issue here. I have a UITableView using custom UITableViewCells. Everything is working as expected except this on particular issue.
Here's the scenario:
I need to remove the "$" symbol in a UITextField right before editing begins. This is done via the textFieldShouldBeginEditing: method.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
LifeEarningsLineItemTableViewCell *cell = (LifeEarningsLineItemTableViewCell *)[self tableViewCellContainingObject:textField inTableView:self.lifeEarningsTableView];
if (textField == cell.itemAmount) {
/*Remove currency symbol for editing.*/
NSString *currencySymbol = [self.currencyFormatter currencySymbol];
NSMutableString *mutableText = [NSMutableString stringWithString:textField.text];
[mutableText replaceOccurrencesOfString:currencySymbol withString:#"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableText length])];
textField.text = mutableText;
}
return YES;
}
Here's the problem:
Between different rows, moving from textField1 to textField2 (in a different row), the "$" is removed, this is GOOD.
Within the same row, moving from textField1 to textField2, the "$" is not removed, this is BAD.
Why is the "$" not being removed within the same row, but does in different rows?
Here's is a visual representation of the issue:
[EDIT...ADDITION]
I get these logs with these flows:
SHOULD BEGIN.....Row:0, Tag:0
SHOULD BEGIN.....Row:0, Tag:1
DID END.....Row:0, Tag:0
DID END.....Row:0, Tag:1
SHOULD BEGIN.....Row:0, Tag:0
SHOULD BEGIN.....Row:1, Tag:1
DID END.....Row:0, Tag:0
DID END.....Row:1, Tag:1
Also, here is the tableViewCellContainingObject:inTableView:tableView method:
- (UITableViewCell *)tableViewCellContainingObject:(UIView *)view inTableView:(UITableView *)tableView {
CGPoint objectRectInTableViewCoordinates = [tableView convertPoint:view.bounds.origin fromView:view];
NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:objectRectInTableViewCoordinates];
return [tableView cellForRowAtIndexPath:cellIndexPath];
}
And the textFieldDidEndEditing method:
- (void)textFieldDidEndEditing:(UITextField *)textField {
LifeEarningsLineItemTableViewCell *cell = (LifeEarningsLineItemTableViewCell *)[self tableViewCellContainingObject:textField inTableView:self.lifeEarningsTableView];
LifeEarningsLineItem *lifeEarningsLineItem = [self.lifeEarningsFetchedResultsController objectAtIndexPath:[self.lifeEarningsTableView indexPathForCell:cell]];
if (textField == cell.itemAmount) {
NSNumber *absInteger = [NSNumber numberWithInteger:abs([textField.text integerValue])];
textField.text = [self.currencyFormatter stringFromNumber:absInteger];
lifeEarningsLineItem.amount = absInteger;
[self sumAmountsAndDisplay];
} else if (textField == cell.itemName) {
lifeEarningsLineItem.name = textField.text;
}
}
I think your solution (decoupling the FRC during editing) may be a little drastic and could have unforeseen effects. Here are a couple of alternative suggestions. I am assuming the problem is caused by the table reloading the row you are editing once you have finished editing the first field in the cell.
Don't store the currency symbol in your model, add it to the displayed text in cellForRowAtIndexPath, if the text field is not editing. When your textfield begins editing, set its text to the value directly from the model. You don't have to do anything in end editing since the reload will add the currency symbol back on for you.
If you dont want to change the model, you can do something similar anyway - in cellForRowAtIndexPath remove the currency symbol if the cell is editing.
Store the index path of the currently editing row and conditionally ignore changes to this row in the FRC delegate method.
Okay, I figured out the problem...it was the gosh darn NSFetchedResultsController delegate methods, specifically the NSFetchedResultsChangeUpdate change type. This is not the first time where NSFetchedResultsController walked all over my table. I should have remembered that NSFetchedResultsController doesn't play well with user-driven edits.
So here is how I have resolved the issue (but I'm still in fear that the whole thing could all come crashing down):
Because of the order of events...
SHOULD BEGIN.....Row:0, Tag:0
SHOULD BEGIN.....Row:0, Tag:1
DID END.....Row:0, Tag:0
DID END.....Row:0, Tag:1
...I couldn't just use a BOOL property to say a text field was in editing mode (the order of events would have to be BEGIN->END->BEGIN->END).
So I created the property:
#property (nonatomic, strong) NSMutableSet *stackOfEditingTextFields;
Then at the start of textFieldShouldBeginEditing: method, I added:
/*Add text field to stack of editing text fields and disable the fetched results controller delegate.*/
[self.stackOfEditingTextFields addObject:textField];
self.lifeEarningsFetchedResultsController.delegate = nil;
Then at the end of textFieldDidEndEditing: method, I added:
/*Remove text field from stack of editing text fields. If stack count is 0, reengage the fetched results controller delegate.*/
[self.stackOfEditingTextFields removeObject:textField];
if ([self.stackOfEditingTextFields count] == 0) self.lifeEarningsFetchedResultsController.delegate = self;
If there's a better suggestion or any unforeseen bi-product bugs that I'm not seeing, I'm all ears.
Thanks for the help lnafziger, in helping me work through it, and suggesting different angles (I +1'd your comments).
I configure my search bar to show the results button, but the button only shows until the user enters a character. At that point, the "X" cancel button replaces it. So without entering characters, the search result set equals the entire data set. I'd like the results button to stay there so when the user has typed enough characters to get a smaller result set (like 5 or 6 rows), they can click the results button, my delegate will get called, and I can show just that result set.
UISearchBar * theSearchBar = [[UISearchBar alloc]
initWithFrame:CGRectMake(0,0,700,40)];
theSearchBar.delegate = self;
theSearchBar.placeholder = #"What are you looking for?";
theSearchBar.showsCancelButton = NO; // shows up after first char typed.
theSearchBar.showsSearchResultsButton = YES; // disappears just when I need it.
...further down in the VC... this method can only called when the search bar's input field is empty.
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar {
NSLog(#" searchBarResultsListButtonClicked for %#",searchBar); //
}
Advice, tutorials, sample code and justified dope-slaps welcome.
TIA
-Mike
#Rayfleck, I think you should not worry about Search Results Button at all.
If what you need is to monitor user's input until they have entered enough characters for filtering:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length]>5) {
[self filterDataWithKeyword:searchText];
[self.tableView reloadData];
} else {
[self resetFilter];
[self.tableView reloadData];
}
}
Here is a partial answer that you can stick in viewDidLoad. It should hide the clear button, but it doesn't keep the results button visible. I'm not sure how the results button view logic is controlled behind the scenes.
for (id subview in mySearchBar.subviews) {
if ([[subview class] isSubclassOfClass:[UITextField class]]) {
[subview setClearButtonMode:UITextFieldViewModeNever];
break;
}
}
Since this approach uses all public APIs your app shouldn't get rejected. Although this approach might be prone to breaking further down the road if/when Apple decides to change the hierarchy of UISearchBar. All I'm doing is looking for the UITextField or subclass and setting its clearButtonMode.
Hope this helps.
I am trying to figure out if there is a way to implement an autocomplete functionality in a UITextField for specific values.
I know that the UITextField can do this using the iPhone dictionary (much like searching google in safari, etc), but I want to be able to programmatically have it correct to certain values that I specify.
How to do this?
I did something very similar to this while working on a recent and rather large project. We had a constantly changing list of auto complete terms and built an auto-complete around them.
First, you'll want to make some type of auto-complete controller. It should take a string and return all possible auto complete terms for that string.
-(NSArray *)completionsForString:(NSString *)myString;
Then, check out the UIMenuController class. It's the class that shows the cut/copy/paste options in many applications. You can get the shared instance of it, populate the menu items yourself, and show it above the text field. The user can then simply tap the term they want.
In the end, the solution worked really well for our needs.
Alternatively, you can use this UITextField subclass (inspired by DOAutocompleteTextField):
https://github.com/hoteltonight/HTAutocompleteTextField
It's got a few more features and is actively developed. The example shows you how to use an array as the data source for the autosuggest text. It takes the same approach as DOAutocompleteTextField, in that it shows the suggested completion text "ghosted" in the text field as the user types.
Have you looked into UISearchDisplayController? There are a few threads here on Stack Overflow, including Core Data references if that is what you are using. Also some alternative methods, elsewhere.
With the help of the aforementioned Ray Wenderlich tutorial, I just implemented a version of this to filter names in an existing UITableView.
I set my text field's delegate as my view controller, my view controller as a UITextFieldDelegate and implemented these two methods:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
NSMutableArray *autoCompleteArray = [[NSMutableArray alloc]init];
[self retrieveData];
for(NSString *curString in _staffTableArray)
{
NSString *lowerCaseCur = [curString lowercaseString];
NSRange substringRange = [lowerCaseCur rangeOfString:substring];
if (substringRange.location == 0)
{
[autoCompleteArray addObject:curString];
}
}
if (![substring isEqualToString:#""])
{
_staffTableArray = [NSMutableArray arrayWithArray:autoCompleteArray];
}
[_staffListTableView reloadData];
}
use this delegate method. you can replace values that you specify.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
if ([string isEqualToString:#"StackO"]) {
textField.text=#"StackOverflow";
}
return YES;
}
Just faced with this thread because I need something similar. How about implementing you own search with the UITextfieldDelegate's method:
- (BOOL)textField:(UITextField *) textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
As you'd probably know this method is called for every UITextfield's typing.