A ViewController consist of two TextFields named textName & textEmail. View Controller loads with cursor on textName. textName contain pre populated word "#gmail.com".
On hitting keyboard's return Key from textName, focus is moved to Textfield textEmail. Here by default, cursor is placed after the word "#gmail.com"
I would like to get the cursor placed at the start. i.e. before #gmail.com
Here is my code. When i hit return key from textName, cursor goes to the beginning of textEmail. But when I tap on textEmail directly, cursor appears after the pre populated word. Please help me!
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.textName{
textEmail.becomeFirstResponder()
let desiredPosition = textEmail.beginningOfDocument
textEmail.selectedTextRange = textEmail.textRangeFromPosition(desiredPosition, toPosition: desiredPosition)
}
if textField == self.textEmail{
dismissViewControllerAnimated(true, completion: nil)
}
return true
}
As a note, I did try editing did begin action for textEmail and added below code but it didn't work either.
let desiredPosition = textEmail.beginningOfDocument
textEmail.selectedTextRange = textEmail.textRangeFromPosition(desiredPosition, toPosition: desiredPosition)
Try this instead:
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.textName{
let beginning = textField.beginningOfDocument
textField.selectedTextRange = textField.textRange(from: beginning, to: beginning)
}
}
So remove textEmail.becomeFirstResponder() and use textField.textRange instead of textEmail.textRangeFromPosition.
And inside of the textFieldDidBeginEditing function use the parameter textField as shown in my example above.
First tou set Delegate UITextFieldDelegate
After you TextField delegate set Self in viewDidLoad
Example
textName.delegate = self
textEmail.delegate = self
then Copy this method in your viewController
func textFieldShouldReturn(textField: UITextField) -> Bool
{
if textField == textName
{
textName.resignFirstResponder()
textEmail.becomeFirstResponder()
textEmail.text = "#gmail.com"
let newPosition = textField.beginningOfDocument
textEmail.selectedTextRange = textEmail.textRangeFromPosition(newPosition, toPosition: newPosition)
}
else if textField == textEmail
{
textEmail.resignFirstResponder()
}
return true
}
Related
Say you have an email field and a cell phone field. If the user fills in the email field, the cell phone field cannot be filled anymore, visa versa.
Here is what I tried below. Making the one field the delegate of the other currently only prevents "Done" from exiting the keyboard, it doesn't make the other text field inactive.
In viewDidLoad
self.cellField.delegate = emailField as? UITextFieldDelegate
then outside
func textFieldShouldBeginEditing(cellField: UITextField) -> Bool {
if emailField.text?.isEmpty == false {
return false
} else {
return true
}
} //// I also tried textFieldDidBeginEditing
Make both
self.cellField.delegate = self
self.emailField.delegate = self
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == cellField {
return emailField.text!.isEmpty
} else if textField == emailField {
return cellField.text!.isEmpty
}
return true
}
I want the X button to show only when
textField.clearButtonMode = .UnlessEditing
When the user pushes the X button, the textView becomes the first responder, and the keyboard pops up.
What do I do so that when the X button is pushed, the text only clears, but the textView does not become focused?
- (BOOL)textFieldShouldClear:(UITextField *)textField
this method is your want,you can check something in it.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006991-CH3-SW10
i hope above link can help you.
If you want it to be focus and able to key in, try
textField.becomeFirstResponder()
If you mean you want it not to show the keyboard and just clear the text, you can delegate your UITextField and use the function to return false:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.text = ""
return false
}
You can implement this delegate like the following:
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.text = ""
return false
}
I happening the following problem:
I have two TextField, one has the default behavior, and on the other, captures the textFieldDidBeginEditing method delegate, to present a UIViewController with PresentationStyle (Custom) and TransitionStyle (CrossDissolve). So far everything works fine. But if I edit the first textField (and leave the keyboard open) and then go to the second, the keyboard is open and I have no way to close it (as if the first textField had lost focus, not even the textFieldShouldReturn is called when I press the button intro).
I tried with:
TextFieldDidEndEditing (for calling resignFirstResponder)
In viewWillDissaper method (I also called resignFirstResponder)
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
print("Resign Last TextField")
textField.resignFirstResponder()
}
func textFieldDidBeginEditing(textField: UITextField) {
print("Did Begin editing")
if textField == self.departing || textField == self.returning{
textField.resignFirstResponder()
self.lastTextFieldSelected = textField
let datePickerViewController = Util.getViewController("DatePickerViewController") as! DatePickerViewController
datePickerViewController.dateStyle = NSDateFormatterStyle.ShortStyle
datePickerViewController.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
datePickerViewController.modalPresentationStyle = UIModalPresentationStyle.Custom
datePickerViewController.datePickerDelegate = self
self.presentViewController(datePickerViewController, animated: true, completion: nil)
}
}
Edit:
I try with self.view.endEditing(true) nothing.
I made a little example (something I check, is that textFieldShouldBeginEditing allows me to close the keyboard.):
https://github.com/Abreu0101/TextFieldBug
It looks like your textFieldDidEndEditing and the other methods are not even called. Make sure you set the UITextField's delegate:
textField.delegate = self
Also import the UITextFielDelegate in your class.
Hope that helps :)
i'm trying to change the default action when user press on a text field. I tried to connect my action at Editing Did Begin event using storyboard as you can see below. The problem is that the keyboard always appear but I want that the keyboard doesn't never appear.
I cannot delete text field delegate methods cause i have others text field in the same view.
How can i do?
Set your view controller as your textField delegate and implement UITextField textFieldShouldBeginEditing delegate method.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == yourTextField {
// implement custom behaviour
return false
}
return true
}
Update
Try this for single textfield keyboard never show
func textFieldDidBeginEditing(textField: UITextField) {
if textField == yourTextField
{
textField.inputView = UIView()
}
}
Simple:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if(textField == YourtextField){
return false
}
return true
}
OR:
in your viewDidLoad() set yourTextField.inputView = UIView()
Works in both ways. Use suitable one.
In the following code I am trying to transfer control from UITextField to the next via a next button.
I am doing this by calling becomeFirstResponder on the next UITextField.
If I don't type anything in the first and current UITextField the next button works as expected. The keyboard stays up and the focus is transferred.
If I do type something, and only if the field is empty. The method becomeFirstResponder for the next field is called and returns true, yet the keyboard is dismissed and focus is not transferred.
public func numberPad(numberPad: APNumberPad, functionButtonAction:UIButton, textInput: UIResponder) {
var current:UITextField?
for field in editCells {
if (current != nil) {
field.valueTextField.becomeFirstResponder()
return;
}
if (field.valueTextField == activeField) {
current = field.valueTextField
}
}
textInput.resignFirstResponder()
}
This function is called when the NEXT or DONE button is pressed on the keyboard. Which is a custom number keypad. APNumberPad specifically.
https://github.com/podkovyrin/APNumberPad
It is my delegate function.
Anyone know any reason becomeFirstResponder would return true and not work, only in some cases, but work in others?
And yes this is the main UI thread. Adding a call to resignFirstResponder on the current field, then a delay and calling becomeFirstResponder works. This causes the keypad to flicker, no matter how small the delay though.
Edit... I am now doing this... and am living with the keyboard flicker for now:
Delay is a helper function for GCD
public func numberPad(numberPad: APNumberPad, functionButtonAction:UIButton, textInput: UIResponder) {
var current:UITextField?
for field in editCells {
if (current != nil) {
current?.resignFirstResponder()
delay (0) {
field.valueTextField.becomeFirstResponder()
}
return;
}
if (field.valueTextField == activeField) {
current = field.valueTextField
}
}
textInput.resignFirstResponder()
}
I don't know if it helps you, or not. I wrote a simple UITextField extension that contains a returnView variable which decides what the textfield should do on return key press:
turn to next text field (if the returnView is an UITextField)
simulate button touch (if the returnView is a UIButton)
or hide keyboard
class NextTextField: UITextField, UITextFieldDelegate {
#IBOutlet weak var returnView: UIView? {
didSet {
if returnView is UITextField {
returnKeyType = UIReturnKeyType.Next
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if let nextTextField = self.returnView as? UITextField {
nextTextField.becomeFirstResponder()
} else if let nextButton = self.returnView as? UIButton {
nextButton.sendActionsForControlEvents(.TouchUpInside)
} else {
self.resignFirstResponder()
}
return true
}
}