I have a number of UITextViews in my ViewController. I am using the textViewDidChange method to handle certain user interaction:
func textViewDidChange(textView: UITextView) { // do something
}
My question is, how do I determine which textview is initiating this function, i.e. which textview is the sender?
you can identify the text views by reference
var textview1 : UITextView!
var textview2 : UITextView!
func textViewDidChange(textView: UITextView) { // do something
switch (textView) {
case textview1: // do something with textview1
case textview2: // do something with textview2
default: break
}
}
or assign tags and identify the views by tag. There are several ways.
You can also try this simple technique.
func textViewDidChange(textView: UITextView) {
//textView(Sender)
if(textView == yourtextview)
{
//do something
}
}
Related
I wanted to know how to save the data in a UITextView once the application is double tapped out/force quitted. Is there some code I can write on the textViewDidChange call that can automatically save the input? The following is my code:
class Coordinator : NSObject,UITextViewDelegate {
var parent : MultiTextField
init(parent1 : MultiTextField) {
parent = parent1
}
func textViewDidBeginEditing(_ textView: UITextView) {
textView.textColor = .black
}
func textViewDidChange(_ textView: UITextView) {
self.parent.obj.size = textView.contentSize.height
}
func textViewDidEndEditing(_ textView: UITextView) {
print("Editing is done")
}
You could save the content in userDefault in textViewDidChange ; and reload content from the userDefault when you load the view.
I have
#IBOutlet weak var messageTextView: UITextView
and I want that when there is a change inside the text then print to console: blabla.
I tried to add the following function, but when I change the text nothing happens:
func textViewDidChange(_ textView: UITextView) {
switch (textView) {
case messageTextView: print("blabla")
default: break
}
}
You need to set the delegate inside viewDidLoad
textView.delegate = self
//
class ViewController: UIViewController , UITextViewDelegate {
i am trying to handle the scrolling of UITextField to avoid the keyboard to overlap it. Click here to view current OUTPUT i am getting ]StoryBoard Screenshot. I tried to scroll the view when i click on the last textfield ..ie password field. When i click on the password field, only the email field ie the 2nd one is scrolling up. No scroll occur ion the first and last field. How can i solve this issue?
`
//
// ViewController.swift
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var name: UITextField!
#IBOutlet weak var email: UITextField!
#IBOutlet weak var password: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Begin
switch textField.tag {
case 0...1:
print("DO NOTHING")
default:
print("Do Scroll")
scrollView.setContentOffset(CGPoint(x:0,y:100) , animated: true)
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
// End
scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Return
if textField.tag == 0
{
email.becomeFirstResponder()
}
else if textField.tag == 1
{
password.becomeFirstResponder()
}
else if textField.tag == 2
{
textField.resignFirstResponder()
}
return true
}
}
`
Unless you really want to handle the logic yourself, I recommend you give IQKeyboardManager a try!
It's a simple CocoaPod library that you import and enable in your appDelegate and well... that's all.
It's a codeless library so you really don't have to think about it ever again as it takes over and handles the repositioning of every textfields in the entire application.
You need to move all textfield to scrollview as below.
There are multiple solutions of the keyboard-overlap problem both here on StackOverflow and on YouTube. They all are variations of editing event handling. For lazy and time-restrained folks (like me), I would recommend just to use a fixed-height footer (a View with or without content) on the bottom of a scroll view. I usually place there different kinds of disclaimers and help information. This footer will always "push" your textview above the keyboard.
I am creating a simple calculator app - everything works fine but I'd like to clear the result label when the user enters a new number in a textfield (or just touches inside a textfield).
Right now I am using a touchesBegan function to clear the label (which also dismisses the keyboard:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
resultLabel.text = ""
}
Problem is of course that it doesn't matter where the user touches on the screen (for example accidentally doesn't hit the calc button), the label will always be cleared.
Is there a way to clear the "resultLabel" when the user touches the "numberField" textfield?
Thanks!
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var numberField: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
numberField.delegate = self
}
extension viewcontroller: UITextFieldDelegate {
func textFieldDidBeginEditing(textField: UITextField) {
resultLabel.text = ""
}
}
For clear resultlable first thing you need to give delegate to your text filed
numberField.delegate = self
Setup your viewcontroller to implement UITextFieldDelegate
Add below method to your viewcontroller
class viewcontroller: UIViewController,UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
resultLabel.text = ""
}
}
You can use text field Delegate methods on your textField where the user is entering the numbers.
textField.delegate = self
textField.tag = 0
Add a tag to your textField and check it in didBeginEditing.
func textFieldDidBeginEditing(textField: UITextField) {
if textField.tag == 0 //Or whatever tag you attach{
//Do the required task
}
}
I'm writing an app for iOS where I have 3 values, and every value is the result of two the others two. So, if you compile two of the three you have the third value.
Right now I have to clear the value inside the Texfield manually before calculate the third value otherwhise the app dont work. What I want to do is that when I compile the first and the second textfield the third clear automatically.
In practise I want to check the last two entered textfield and clear the other.
How can I check this statement in swift?
Thank you
try to use TextFieldDelegate for this, in the case you are training to display this may help
class ViewController: UIViewController,UITextFieldDelegate //set delegate to class
#IBOutlet var TF1: UITextField
#IBOutlet var TF2: UITextField
#IBOutlet var TF3: UITextField
override func viewDidLoad() {
super.viewDidLoad()
TF1.delegate = self //set delegate to textfile
TF2.delegate = self //set delegate to textfile
TF3.delegate = self //set delegate to textfile
}
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
return false
}
func textFieldShouldReturn(textField: UITextField!) -> Bool { //delegate method
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
if(TF1.text != "" && TF2.text != ""){
TF3.text = ""
}
}
I hope this can help you