无法连接到 UITextField [英] Cannot connect to UITextField

查看:29
本文介绍了无法连接到 UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个基本的货币转换应用程序以尝试学习 ios 开发,但不知道如何监听 UITextField 事件.到目前为止,这是我的代码:

I am making a basic currency conversion app in an attempt to learn ios development, but cannot figure out how to listen to UITextField events. this is my code so far:

//
//  ViewController.swift
//  Currency
//

import UIKit

class ViewController: 

      UIViewController

    , UIPickerViewDelegate
    , UIPickerViewDataSource 

    , UITextViewDelegate

    , UITextFieldDelegate

    {

    @IBOutlet var pickerView     : UIPickerView!
    @IBOutlet var inputTextField : UITextField!
    @IBOutlet weak var outputText: UILabel!


    // definface pickerdata : note it's a constant
    let pickerData = ["euro", "us-dollar", "yen", "yuan", "peso"]

    // initialize src and tgt currency with listeners
    // note not sure what to do with these observers yet
    var src : String = "" // { did set {} }
    var tgt : String = "" 

    override func viewDidLoad() {

        super.viewDidLoad()

        // instance of this class is source of data
        pickerView.dataSource   = self
        pickerView.delegate     = self

        // match default srce and tgt to ios default
        src = pickerData[0]
        tgt = pickerData[0]

        // disable spell check
        inputTextField.autocorrectionType = UITextAutocorrectionType.no

    }

    //MARK: -  UIPickerView data source method
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        // number of components in pickerData
        return 2
    }

    // UIPickerViewDelegate methods
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        // number of rows
        return pickerData.count
    }

    //MARK: Delegates - places data into picker
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[row]
    }

    // responding to row selections and also need to write to UILabel
    // NOTE: we need to 
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if component == 0 { src = pickerData[row] } 
        else              { tgt = pickerData[row] }

        let rate = Data.getExchangeRate(inputUnit: src, outputUnit: tgt)

        print (">> (src,tgt): (", src, ",", tgt, ")   value: ", inputTextField.text)
        outputText.text = "hello world"

    }


    //MARK: - UITextFieldDelegate  methods
    func textFieldShouldBeginEditing(_ textField : UITextField) -> Bool{
        print("TextField did begin editing method called")
        return true
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

        print("textField changing")
        return true
    }    

}

//MARK: - UITextFieldDelegate methods 下的函数应该在我编辑文本字段时触发,但现在没有这样做.有什么想法吗?

The functions under //MARK: - UITextFieldDelegate methods should fire when I edit the text field, but is not doing so right now. Any thoughts?

推荐答案

delegate 是一个对象,当另一个对象遇到一个事件时,它代表另一个对象采取行动程序.委托对象通常是 responder 对象.

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. The delegating object is often a responder object.

委托对象保持对另一个对象(委托)的引用,并在适当的时候向它发送消息.该消息通知委托对象即将处理或刚刚处理的事件的委托.

The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled.

委托可以通过更新自身或应用程序中其他对象的外观或状态来响应消息.

The delegate may respond to the message by updating the appearance or state of itself or other objects in the application.

委托类有一个插座或属性,通常是一个名为委托的.委托类在 NSObject 的类别上声明方法,并且委托只实现那些它有兴趣与委托对象协调或影响该对象的默认行为的方法.

The delegating class has an outlet or property, usually one that is named delegate. The delegating class declares methods on a category of NSObject, and the delegate implements only those methods in which it has an interest in coordinating itself with the delegating object or affecting that object’s default behavior.

我希望现在你明白为什么不调用文本字段委托方法了.由于您尚未将 ViewController 指定为 inputTextField 的委托.因此,刚刚处理了 inputTextField 的消息不会传递到您的 ViewController.

I hope now you get why textfield delegate methods were not called. As you have not assigned your ViewController as delegate of inputTextField. Hence the message that inputTextField is just handled is not delivered to your ViewController.

您可以查看这个 链接以了解有关 Delegate 的更多信息.要了解委托模式的工作原理,请查看this 链接.

You can check this link to know more about Delegate. To know how delegation pattern works check this link.

您可以将您的班级设置为文本字段的委托

You can set your class as delegate of textfield as

inputTextField.delegate = self;

-----编辑----

-----EDIT----

如何检测文本字段中当前的文本是什么?

how to detect what the current text is in the text field?

在 viewDidLoad 添加

In viewDidLoad add

inputTextField.addTarget(self, action: #selector(typingInput), for: .editingChanged)

将方法定义为

func typingInput(textField:UITextField){
    if let typedText = textField.text {        
        print(typedText ) // get the current string
    }
}

感谢这个答案.

这篇关于无法连接到 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆