处理测试更改文本字段中的事件swift

时间:2017-01-22 09:45:59

标签: ios swift uitextfield uitextview

我正在构建一个小型iOS应用程序,它涉及在tex字段中的文本发生更改时执行某些操作。我一直试图弄清楚如何在测试发生更改事件时编写某个函数来调用,但这些解决方案都不适合我,我相信它们已经过时了。最受欢迎的解决方案是:

func textFieldDidChange(_ textField: UITextField) {

}

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!

}

此解决方案已发布在此处:How do I check when a UITextField changes?

但是,当我尝试将此实现到我的代码时,我收到一条错误,指出成员UITextView的值没有成员addTarget。我不确定代码是否错误或者我是在错误的地方写的,我仍然学得很快,所以如果有明显的错误,我很抱歉。 这是我的代码:

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!


}

修改

我尝试过这样做,这几乎就是解决方案的副本,但是当文本视图中的文本发生变化时,语句仍然无法打印

<?php

namespace app\models;
use yii\base\Model;



class RetrievePasswordForm extends Model
{
    public $email;

    public function rules()
    {
        return [
            ['email', 'required'],
            ['email', 'email'],
        ];
    }
}

有人可以帮忙吗?

编辑2: 我想出了问题,这不是在这段代码中。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

您的TextField变量(应为textField - 使用小写首字母表示变量,大写首字母表示类)是UITextView,而不是UITextField所以你试图使用不正确的解决方案。

对于UITextView,您需要实施相应的UITextViewDelegate方法:

class JournalEntryViewController: UIViewController, UITextViewDelegate
{

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

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