UIKeyboard not appearing when tapping on UITextField
Following will fix the keyboard issue
Simulator -> Hardware -> Keyboard -> Toggle Software Keyboard should solve this problem.
UITextField in UITableView post 3.0 should automatically support scrolling above the keyboard?
My problem was that my class was a subclass of UIViewController when it needed to be UITableViewController.
UITextField replace text with NSUndoManager Support
Shake-to-undo
Put this line in your appDelegate's application:didFinishLaunchingWithOptions:
method:
application.applicationSupportsShakeToEdit = YES;
and in the relevant viewController.m
-(BOOL)canBecomeFirstResponder {
return YES;
}
the rest of this code goes in viewController.m
Property
Put this in the class extension...
@interface myViewController()
@property (weak, nonatomic) IBOutlet UITextField *inputTextField;
@end
link it up to your text field in Interface Builder.
Undo method
Adds an invocation of itself to the redo stack
- (void)undoTextFieldEdit: (NSString*)string
{
[self.undoManager registerUndoWithTarget:self
selector:@selector(undoTextFieldEdit:)
object:self.inputTextField.text];
self.inputTextField.text = string;
}
(we do not need to create an NSUndoManager instance, we inherit one from the UIResponder superclass)
Undo actions
Not required for shake-to-undo, but could be useful...
- (IBAction)undo:(id)sender {
[self.undoManager undo];
}
- (IBAction)redo:(id)sender {
[self.undoManager redo];
}
Invocations of the undo method
Here are two different examples of changing the textField's contents…
Example 1
Set the textField's contents from a button action
- (IBAction)addLabelText:(UIButton*)sender {
[self.undoManager registerUndoWithTarget:self
selector:@selector(undoTextFieldEdit:)
object:self.inputTextField.text];
self.inputTextField.text = @"text";
}
Risking loss of clarity, we can shorten this to:
- (IBAction)addLabelText:(UIButton*)sender {
[self undoTextFieldEdit: @"text"];
}
as the undoManager invocation is the same in both methods
Example 2
Direct keyboard input editing
#pragma mark - textField delegate methods
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.undoManager registerUndoWithTarget:self
selector:@selector(undoTextFieldEdit:)
object:textField.text];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//to dismiss the keyboard
[textField resignFirstResponder];
return YES;
}
in iOS 8 when clicking on uitextfield keyboard is not open
In your iOS simulator, make sure that you've unchecked Hardware > Keyboard > Connect Hardware Keyboard.
No consecutive spaces Swift UITextField
Just check the previous and the current character entered by the user.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text?.characters.last == " " && string == " "{
// If consecutive spaces entered by user
return false
}
// If no consecutive space entered by user
return true
}
Related Topics
Find Attributes from Attributed String That User Typed
Button Action in Mkannotation View Not Working
Programmatically Go Back to Previous Viewcontroller in Swift
iOS 11 Safe Area Layout Guide Backwards Compatibility
How to Call a View Controller Programmatically
How to Split String into Substrings on iOS
App Store Connect Message: Your Account Will Soon Need to Be Migrated to Federated Auth
How to Do Authentication in Uiwebview Properly
@Property/@Synthesize Equivalent in Swift
Uitextfield -Webview No Longer Supported
Delivery Report of Sending Remote Push Notifications Using Apns - iOS
Switch Universal App to iPhone Only App
Run iPhone as an Ibeacon in the Background
Contentview Not Indenting in iOS 6 Uitableviewcell Prototype Cell
Convert Coordinates to City Name
Fatal Error: Init(Coder:) Has Not Been Implemented Error Despite Being Implemented