I want to hide keyboard before move to next controller. Also implement self.view.enditing(true) in viewWillDisapear . But keybard not hide how to hide this?
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == tagTxt{
self.view.endEditing(true)
textField.resignFirstResponder()
let interstControlle = self.storyboard?.instantiateViewController(withIdentifier: "InterestViewController") as! InterestViewController
interstControlle.PContId = "InterestViewController"
interstControlle.delegate = self
if tagsID.count > 0{
interstControlle.PTagsID = tagsID
}
self.navigationController?.pushViewController(interstControlle, animated: true)
}
}
Use this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if textField == tagTxt{
return NO;
}
return YES;
}
Swift 3:
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == tagTxt{
return false
}
return true
}
You have to return false before textfield get active. We can see difference by name textFieldShouldBeginEditing and textFieldDidBeginEditing.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == tagTxt {
//Your code to move in next view
return false
}
return true
}
Related
Help me, plz!
I can't not hide keyboard when changing one to another TextField.
I click on the "user" textfield pop-up Picker , click on a "Password" textfield , a pop-up keyboard, but if you click again on the "User" is not hide keyboard.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
userNameTextField.resignFirstResponder()
userPasswordTextField.resignFirstResponder()
}
func textFieldDidBeginEditing(textField: UITextField) {
if textField == userNameTextField {
pickerUser.hidden = false
print("userNameTextField")
} else {
pickerUser.hidden = true
print("#userPasswordTextField")
}
}
#IBAction func userNameTextFieldStartEdit(sender: AnyObject) {
userNameTextField.resignFirstResponder()
userPasswordTextField.resignFirstResponder()
pickerUser.hidden = false
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Use this Code,
func textFieldDidBeginEditing(textField: UITextField) {
if textField == userNameTextField {
pickerUser.hidden = false
print("userNameTextField")
self.view.endEditing(true)
} else {
pickerUser.hidden = true
print("#userPasswordTextField")
}
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if(textField == userNameTextField) {
self.view.endEditing(true)
return true
}
return true
}
its working for me, hope its helpful
tyr only one line add in below:
func textFieldDidBeginEditing(textField: UITextField) {
if textField == userNameTextField {
textField.resignFirstResponder() // this line add
pickerUser.hidden = false
print("userNameTextField")
} else {
pickerUser.hidden = true
print("#userPasswordTextField")
}
}
I think it's that function you should call to hide your keyboard :
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}
Instead of your pickerUser.hidden = true
And also:
class ViewController: UIViewController, UITextFieldDelegate {
(Do you have that one ?)
func textFieldDidBeginEditing(textField: UITextField) {
scrlView.setContentOffset(CGPointMake(0, textField.frame.origin.y-70), animated: true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
scrlView .setContentOffset(CGPointMake(0, 0), animated: true)
return true
}
How can I move cursor from one textfield to another automatically after entering value in the first textfield?
first, you need add tags to your text fields orderly
func textFieldShouldReturn(textField: UITextField) -> Bool {
let nextTag = textField.tag + 1;
// Try to find next responder
var nextResponderTxt: UITextField!
nextResponderTxt = textField.superview?.viewWithTag(nextTag) as? UITextField
if ((nextResponderTxt) != nil) {
// Found next responder, so set it.
nextResponderTxt.becomeFirstResponder()
} else {
// Not found, so remove keyboard.
textField.resignFirstResponder()
}
return false; // We do not want UITextField to insert line-breaks.
}
Also you can change the return key to next
When I tap on this UITextFieldmentioned on screen below, a numpad keyboard appears, the problem is that when I start tap on number, my UITextField do not update, stay with no number, only placeholder.
What I need to do?
Thanks
ADDING CODE!
#IBOutlet weak var salarioTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
updateView()
salarioTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
if textField == feriasTextField
{
feriasTextField.inputView = feriasPicker
}
else if textField == salarioTextField
{
salarioTextField.keyboardType = UIKeyboardType.NumberPad
}
else if textField == inicioTextField
{
textField.inputView = datePicker
textField.inputAccessoryView = toolbar
datePicker.date = inicioDate
}
else if textField == motivoTextField
{
motivoTextField.inputView = motivoPicker
}
else
{
textField.inputView = datePicker
textField.inputAccessoryView = toolbar
datePicker.date = fimDate
}
return true
}
func textFieldDidBeginEditing(textField: UITextField)
{
backgroundScrollView.scrollEnabled = true
let scrollSize = CGSizeMake(view.frame.width, view.frame.height)
backgroundScrollView.contentSize = scrollSize
activeTextField = textField
activeTextField?.becomeFirstResponder()
}
func textFieldDidEndEditing(textField: UITextField)
{
backgroundScrollView.contentSize = CGSizeMake(0, 800)
backgroundScrollView.scrollEnabled = true
}
Please ignore the others UITextField.
Make sure you have enabled user interaction for the field.
If you implement the delegate method textViewDidBeginEditing, you can put a breakpoint or a print statement here to see if it triggers.
You can try calling becomeFirstResponder on the field.
I just figured out what was happening.
I was returning false to the method below.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
return true;
}
Just changed it to true and it's working.
Thanks for your help.
I have an app built with Swift and iOS8. In my ViewController I had two textfields.
I use this code:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.textfield_A {
self.textfield_B.becomeFirstResponder()
}
if textField == self.textfield_B {
self.textfield_B.resignFirstResponder()
}
return true
}
The effect:
I select textfield_A and Press the Next Button -> the cursor jump to textfield_B
On textfield_B I press the Done Button -> the keyboard will be hidden.
All works fine.
But now I have made a change and the code doesn't work like this anymore.
I changed the textfield_A to a textView.
Any idea how I have to modify my code?
You have to add an extension, this is the extension for swift 3.0
extension UITextField {
class func connectFields(fields:[UITextField]) -> Void {
guard let last = fields.last else {
return
}
for i in 0 ..< fields.count - 1 {
fields[i].returnKeyType = .next
fields[i].addTarget(fields[i+1], action: #selector(UIResponder.becomeFirstResponder), for: .editingDidEndOnExit)
}
last.returnKeyType = .done
last.addTarget(last, action: #selector(UIResponder.resignFirstResponder), for: .editingDidEndOnExit)
}
}
and after you add this line of code:
UITextField.connectFields(fields: [field1, field2, field3])
textFieldShouldReturn won't be called anymore for textfield_A now because is is a textview now, not a textfield.
Try adding this function
func textView(textView: UITextView!, shouldChangeTextInRange: NSRange, replacementText: NSString!) -> Bool {
if(replacementText == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
If you are using both textfield then below code might be helpful to you my friend
Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like).
The method might look something like this:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.field1 {
self.field2.becomeFirstResponder()
}
return true
}
And do not forget to give Delegate
TextField.delegate = self
Using textField you can detect return key press by using textFieldShouldReturn: method. textView is a multi-line input so return key just adds a new line, so the easiest way to catch return key press is to implement UITextViewDelegate method:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
You can do it this way in your doneButton action:
#IBAction func donePressed(sender: AnyObject) {
if textV.resignFirstResponder() { //check if cursor is at textView.
textfield_B.becomeFirstResponder() //move it to your next textField.
} else {
textfield_B.resignFirstResponder() //else hide your keyboard.
}
}
And your result will be:
Hope it helps.
Swift 4.2
This is a More Generic Solution you can use this code with any amount of TextFields.
Just inherit UITextFieldDelegate and update the Textfield Tag according to the order
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let txtTag:Int = textField.tag
if let textFieldNxt = self.view.viewWithTag(txtTag+1) as? UITextField {
textFieldNxt.becomeFirstResponder()
}else{
textField.resignFirstResponder()
}
return true
}
I have a button and text textfield in my view. when i click on the textfield a keyboard appears and i can write on the textfield and i also able to dismiss the keyboard by clicking on the button by adding:
[self.inputText resignFirstResponder];
Now I want to enable return key of keyboard. when i will press on the keyboard keyboard will disappear and something will happen. How can I do this?
Ensure "self" subscribes to UITextFieldDelegate and initialise inputText with:
self.inputText.delegate = self;
Add the following method to "self":
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.inputText) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
Or in Swift:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == inputText {
textField.resignFirstResponder()
return false
}
return true
}
With extension style in swift 3.0
First, set up delegate for your text field.
override func viewDidLoad() {
super.viewDidLoad()
self.inputText.delegate = self
}
Then conform to UITextFieldDelegate in your view controller's extension
extension YourViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == inputText {
textField.resignFirstResponder()
return false
}
return true
}
}
While the other answers work correctly, I prefer doing the following:
In viewDidLoad(), add
self.textField.addTarget(self, action: #selector(onReturn), for: UIControl.Event.editingDidEndOnExit)
and define the function
#IBAction func onReturn() {
self.textField.resignFirstResponder()
// do whatever you want...
}
Just add a target on textField setup function on viewDidLoad, then add its related function as selector.
override func viewDidLoad() {
super.viewDidLoad()
textField.addTarget(self, action: #selector(textFieldShouldReturn(sender:)), for: .primaryActionTriggered)
}
#objc func textFieldShouldReturn(sender: UITextField) {
textField.resignFirstResponder()
}
Use Target-Action UIKit mechanism for "primaryActionTriggered" UIEvent sent from UITextField when a keyboard done button is tapped.
textField.addTarget(self, action: Selector("actionMethodName"), for: .primaryActionTriggered)