Unable to delegate textfield with UipickerView in SWIFT - swift

i'm currently facing a problem in swift.
I implemented a method to make textfield next responder on returnkey tap, and o make it fonctional i have to make my textfields delegate (everything work on my other class).
The problem is, i also implemented a UIPickerView , which appear when "textfieldcategorie" is tapped, this pickerview need to be delegate, to put the choosen value in the textfield.
by the way, the method to make textfield nextresponder not working anymore, at the moment where i put the textfield in delegate , it become inactive.
this is the code is used :
declaration of my variable :
#IBOutlet var textfieldcategorie: UITextField!
#IBOutlet var keyword: UITextField!
#IBOutlet var codepostal: UITextField!
#IBOutlet var prix: UITextField!
#IBOutlet var pickercategorie: UIPickerView! = UIPickerView()
The method to show uipickerview on tap :
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickercategorie.hidden = false
return false
}
The method to make textfield nextresponder :
func textFieldShouldReturn(textField: UITextField) -> Bool {
//delegate method
if textField == keyword {
textfieldcategorie.becomeFirstResponder()
}
return true
}
}
I don't know if it's important, but i also use theses methods :
to move up the textfield when the keyboard appears :
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 25)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 25)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
var movementDuration:NSTimeInterval = 0.3
var movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
//Fin Monter la vue lorsque le clavier apparait
And to dismiss the keyboard on tap anywhere in the view:
func DismissKeyboard(){
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
Thank's

I feel you must change your code as follows:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//delegate method
if textField === keyword {
textfieldcategorie.becomeFirstResponder()
}
return true
}
The reason being == operator checks equality of two objects but === operator check identity of two objects i.e. wether these two refer to the same object.
Also pleas change:
#IBOutlet var pickercategorie: UIPickerView!

Related

Swift How to disable a TextField while another UITextField is highlighted or has value

I have three text fields: tempTextField, airPressureTextField, and airDensityTextField. I want to create a check between Textfields to get the following logic:
If tempTextField or airPressureTextField is in focus where user trying to enter a value or user enters value and returns, I want to disable airDensityTextField. I tried this code. It disables airDensityTextField correctly. But If I remove the focus from tempTextField or airPressureTextField and empty both those two textfields, the airDensityTextField is still disabled.
func textFieldDidEndEditing(_ textField: UITextField) {
if let t = tempTextField.text, let ap = airPressureTextField.text {
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
I tried the code in both textFieldDidEndEditing and textFieldDidBeginEditing, the result is the same.
How can I make it work?
You can see which textfield is editing inside textFieldDidBeginEditing and disable airDensityTextField. Then, when they finish editing, you need to check if the text is "" (blank) in addition to whether it's nil or not.
class TextFieldVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tempTextField: UITextField!
#IBOutlet weak var airPressureTextField: UITextField!
#IBOutlet weak var airDensityTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tempTextField.delegate = self
airPressureTextField.delegate = self
airDensityTextField.delegate = self
}
func textFieldDidBeginEditing(_ textField: UITextField) {
switch textField {
case tempTextField:
airDensityTextField.isEnabled = false
case airPressureTextField:
airDensityTextField.isEnabled = false
default:
break
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
if
let tempText = tempTextField.text,
let apText = airPressureTextField.text,
tempText != "" || apText != "" /// check if at least one of them has text
{
airDensityTextField.isEnabled = false
} else {
airDensityTextField.isEnabled = true
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}

How to come back in previous textfield if all textfield is empty?

[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
import UIKit
class OTPVC: CommonViewController,UITextFieldDelegate {
#IBOutlet weak var aboutOtp: UIView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var bottomConstraint: NSLayoutConstraint!
#IBOutlet weak var firstOtptxt: UITextField!
#IBOutlet weak var secondOtptxt: UITextField!
#IBOutlet weak var thirdOtptxt: UITextField!
#IBOutlet weak var fourthOtptxt: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setUpView()
firstOtptxt.delegate = self
secondOtptxt.delegate = self
thirdOtptxt.delegate = self
fourthOtptxt.delegate = self
}
private func setUpView() {
aboutOtp.makeCornerRadius(10)
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if ((textField.text?.count)!) < 1 && (string.count > 0) {
if textField == firstOtptxt{
secondOtptxt.becomeFirstResponder()
}
if textField == secondOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
fourthOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
fourthOtptxt.becomeFirstResponder()
}
textField.text = string
return false
} else if (((textField.text?.count)!) >= 1) && (string.count == 0) {
if textField == secondOtptxt {
firstOtptxt.becomeFirstResponder()
}
if textField == thirdOtptxt {
secondOtptxt.becomeFirstResponder()
}
if textField == fourthOtptxt {
thirdOtptxt.becomeFirstResponder()
}
if textField == firstOtptxt {
firstOtptxt.becomeFirstResponder()
}
textField.text = string
return false
}
else if ((textField.text?.count)!) >= 1 {
textField.text = string
return false
}
return true
}
}
[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
[if all textfield is fill it will easily come back but if all textfield is empty and i click on last textfield then i want to come back to the previous textfield and so on how i achieved this please help me ]
protocol MyTextFieldDelegate: UITextFieldDelegate {
func textField(_ textField: UITextField, didDeleteBackwardAnd wasEmpty: Bool)
}
class MyTextField: UITextField {
override func deleteBackward() {
// see if text was empty
let wasEmpty = text == nil || text! == ""
// then perform normal behavior
super.deleteBackward()
// now, notify delegate (if existent)
(delegate as? MyTextFieldDelegate)?.textField(self, didDeleteBackwardAnd: wasEmpty)
}
}
[just put a delegate method and its work fine in my project yippy]
there is a trick, you can have a 'space' in your textfields, so that problem will be solved .

How start to UITextView from paragraph in Swift

How to start a new line in UITextView with a little space, called paragraph I believe?
What you need to do is make the controller confront to UITextFieldDelegate
for example:
class ViewController: UIViewController,UITextFieldDelegate
{
//link the textfield
#IBOutlet weak var textField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
// link the delegate to the textfield
textField.delegate = self
}
//this function detects the return button when it's pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
//since we always want to start a new line with space we override the default function
textField.text += "\n "
return false
}

Dismiss UITextField keyboard?

I have multiple textfields
#IBOutlet var emailLogin: UITextField!
#IBOutlet var passLogin: UITextField!
#IBOutlet var emailSignUp: UITextField!
#IBOutlet var passSignUp: UITextField!
as of now these aren't really needed, because of how I am dismissing it by tapping anywhere on the screen, however I also want it to dismiss when I press the return key.
override func viewDidLoad() {
super.viewDidLoad()
self.emailLogin.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
First question: When I need to dismiss with only one I would set the delegate like so, but how do I handle this when I have multiple views that need to be dismissed on return key?
Also, there are two separate views, but both use the same class. Is this a problem for what I am trying to do?
From here
func dismissKeyboard() {
view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
This dismisses the keyboard but only for the textfield that I set as self.
Any info is appreciated!
EDIT:
Change your code to the following and it should work:
#IBOutlet var emailLogin: UITextField?
#IBOutlet var passLogin: UITextField?
#IBOutlet var emailSignUp: UITextField?
#IBOutlet var passSignUp: UITextField?
emailLogin?.delegate = self
passLogin?.delegate = self
emailSignUp?.delegate = self
passSignUp?.delegate = self
The IBOutlets from other class were not initialized when your LoginViewController loads, thus end up with unwrapping nil objects, which is not allowed in Swift. (You should be able to see that in your console output in Xcode.) Use optional variables will prevent that from happening.
Use ViewControllerDelegate to handle tap outside of any textField and textView. It will dismiss keyboard when you tap on outside of textField. Put below code in your view controller:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent:event)
self.view.endEditing(true)
}
You don't need to add TapGesture now to handle this.

Swift: Chain Textfields for Next Button

How does one simply chain up a few textfields for a form?
I found an old Post for obj-C here and unsuccessfully tried to "swift it".
What I tried to do:
#IBOutlet weak var nameTextfield: UITextField!
#IBOutlet weak var emailTextfield: UITextField!
nameTextfield.addTarget(target: emailTextfield, action: becomeFirstResponder(), forControlEvents: UIControlEvents.EditingDidEndOnExit)
Do you have to target the textfield in an other way than by its IBOutlet?
add this function:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
After return is hit on each textfield the keyboard will resign. You can add tags to your textfields and then add if/else statements for different behaviors depending on which textField.tag responded if you wish.
You do not have to target the textfield by anything other than the IBOutlet for this function.
Try out this code.
func textFieldShouldReturn(textField: UITextField) -> Bool
{
if (usernameTF.resignFirstResponder())
{
passwordTF.becomeFirstResponder()
}
textField.resignFirstResponder();
return true
}