Kareem Aboughazala
Kareem Aboughazala

Reputation: 545

Handling test changed events in a text field swift

I am building a small iOS application that involves doing certain things when text in a tex fields changes. I have been trying to figure out how I can write certain function to be called when the test changed event occurs but none of those solutions work for me, I believe they are outdated. the most popular solution is this:

textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

and

func textFieldDidChange(_ textField: UITextField) {

}

This solution was posted here: How do I check when a UITextField changes?

However when I try to implement this to my code I get an error saying that value of member UITextView has no member addTarget. I am not sure if the code is wrong or if I'm writing it in the wrong place, I am still learning swift so I'm sorry if there's an obvious error. Here is my code:

import UIKit

class JournalEntryViewController: UIViewController
{



    override func viewDidLoad() {
        super.viewDidLoad()
        print(RatingSelection.selectedSegmentIndex)
        let date = NSDate()
        let calendar = NSCalendar.current
        let hour = calendar.component(.hour, from: date as Date)
        let minutes = calendar.component(.minute, from: date as Date)
        print("minutes: ",minutes)
        if (minutes == 16)
        {
            print("got in!!!")
            print(TextField.text)

        }


        func textFieldDidChange(_ textField: UITextField)
        {
            print("text changed")

        }

// This line is the problem, I'm trying to add a text changed event to TextField 
        TextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)





        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBOutlet weak var TodaysDate: UILabel!

    @IBOutlet weak var RateDayLabel: UILabel!

    @IBOutlet weak var RatingSelection: UISegmentedControl!

    @IBOutlet weak var TextField: UITextView!

}

EDIT:

I tried doing this, which is almost a copy of the solution but the statement still doesn't get printed when the text changes in the textview

class JournalEntryViewController: UIViewController, UITextViewDelegate
{


    override func viewDidLoad()
    {

        super.viewDidLoad()
        self.textField.delegate = self

        func textViewDidChange(_ textView: UITextView)
        {
            print("text changed!!!")
        }


    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }


    @IBOutlet weak var textField: UITextView!

    @IBOutlet weak var todaysDate: UILabel!

    @IBOutlet weak var rateDayLabel: UILabel!

    @IBOutlet weak var ratingSelection: UISegmentedControl!


}

Can anyone please help?

EDIT 2: I figured out the problem, it wasn't in this code. Thanks for the help!

Upvotes: 0

Views: 337

Answers (1)

Paulw11
Paulw11

Reputation: 115002

Your TextField variable (which should be textField - use a lower case first letter for variables, upper case first letter for classes) is a UITextView, not a UITextField so you are attempting to use an incorrect solution.

For a UITextView you need to implement the appropriate UITextViewDelegate method:

class JournalEntryViewController: UIViewController, UITextViewDelegate
{

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        self.textField.delegate = self
    }

    func textViewDidChange(_ textView: UITextView) {
       // Do whatever
    }
}

Upvotes: 2

Related Questions