UITextField Autocomplete?

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 … Read more

How can I limit the number of decimal points in a UITextField?

Implement the shouldChangeCharactersInRange method like this: // Only allow one decimal point // Example assumes ARC – Implement proper memory management if not using. – (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSArray *arrayOfString = [newString componentsSeparatedByString:@”.”]; if ([arrayOfString count] > 2 ) return NO; return YES; } This creates … Read more

Unable to add UITextField to UIAlertView on iOS7…works in iOS 6

You can’t easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it. One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. … Read more

Programmatically Select all text in UITextField

Thats what did the trick for me: [self.titleField setSelectedTextRange:[self.titleField textRangeFromPosition:self.titleField.beginningOfDocument toPosition:self.titleField.endOfDocument]]; Pretty ugly but it works, so there will be no sharedMenuController shown! To fix the “only works every second time” problem use following: __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ __strong __typeof(weakSelf) strongSelf = weakSelf; UITextRange *range = [strongSelf textRangeFromPosition:strongSelf.beginningOfDocument toPosition:strongSelf.endOfDocument]; [strongSelf setSelectedTextRange:range]; }); … Read more

How to dismiss keyboard iOS programmatically when pressing return

The simple way is to connect the delegate of UITextField to self (self.mytestField.delegate = self) and dismiss the keyboard in the method textFieldShouldReturn using [textField resignFirstResponder]; Another way to dismiss the keyboard is the following: Objective-C [self.view endEditing:YES]; Swift: self.view.endEditing(true) Put [self.view endEditing:YES]; where you would like to dismiss the keyboard (Button event, Touch event, … Read more

self.delegate = self; what’s wrong in doing that?

See this thread http://www.cocoabuilder.com/archive/cocoa/241465-iphone-why-can-a-uitextfield-be-its-own-delegate.html#241505 Basically, the reason for the “freeze” when you click on your UITextField with itself as a delegate is that respondsToSelector is calling itself -> infinite recursion. UITextField is unique AFAIK. You can usually use a class as its own delegate with no particular problems. For UITextField you must create an actual … Read more