简体   繁体   English

在文本字段中快速处理测试更改的事件

[英]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. 我正在构建一个小的iOS应用程序,其中涉及在tex字段中的文本更改时执行某些操作。 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? 此解决方案发布在这里: 如何检查UITextField何时更改?

However when I try to implement this to my code I get an error saying that value of member UITextView has no member addTarget. 但是,当我尝试在代码中实现此功能时,出现错误,提示成员UITextView的值没有成员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 我尝试这样做,这几乎是解决方案的一个副本,但是当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. 编辑2:我想出了问题,不在代码中。 Thanks for the help! 谢谢您的帮助!

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. 您的TextField变量(应为textField变量使用小写的首字母,类使用大写的首字母)是UITextView ,而不是UITextField因此您尝试使用错误的解决方案。

For a UITextView you need to implement the appropriate UITextViewDelegate method: 对于UITextView您需要实现适当的UITextViewDelegate方法:

class JournalEntryViewController: UIViewController, UITextViewDelegate
{

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM