【问题标题】:Handling test changed events in a text field swift快速处理文本字段中的测试更改事件
【发布时间】:2017-01-22 09:45:59
【问题描述】:

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

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

和

func textFieldDidChange(_ textField: UITextField) {

}

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

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

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!

}

编辑:

我试过这样做,这几乎是解决方案的副本,但是当 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!


}

有人可以帮忙吗?

编辑 2: 我发现了问题所在,它不在此代码中。 感谢您的帮助!

【问题讨论】:

    标签: ios swift uitextfield uitextview


    【解决方案1】:

    您的 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
        }
    }
    

    【讨论】:

    • 您已将委托方法放入 viewDidLoad。它应该在任何其他功能之外
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2022-11-04
    • 2017-09-21
    • 2019-04-08
    • 1970-01-01
    相关资源
    最近更新 更多