Swift:编辑NSTextField时的调用函数

时间:2017-01-08 13:50:23

标签: swift macos nstextfield

我尝试使用NSTextField的更改用户输入动态更新其他视图的文本。以下方法不适用,因为它适用于iOS。是否有类似osx NSTextField可用的内容?

self.equationTextField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: UIControlEvents.editingChanged)
//build error: "Use of unresolved identifier 'UIControlEvents'"

谢谢!

2 个答案:

答案 0 :(得分:10)

您必须使用NSControlTextEditingDelegate。如果您使用的是故事板,请将NSTextField delegate连接到ViewController。如果您以编程方式创建它,只需将其设置为如下代码:textfield.delegate = self中的viewDidLoad()

NSControlTextEditingDelegate中,您可以使用controlTextDidChange在编辑文本时获取通知。

extension ViewController: NSControlTextEditingDelegate {
    override func controlTextDidChange(_ notification: Notification) {
        if let textField = notification.object as? NSTextField {
            print(textField.stringValue)
            //do what you need here
        }
    }
}

答案 1 :(得分:2)

  • 我发现Swift 4问题
  • textfield.delegate =自我
  • AND
  • NSControlTextEditingDelegate

  • controlTextDidChange和controlTextDidEndEditing位于 NSControlTextEditingDelegate

  • 这是的父协议 NSTextFieldDelegate协议NSTextFieldDelegate: NSControlTextEditingDelegate
  • 因此VC应该实现NSTextFieldDelegate而不是NSControlTextEditingDelegate

class  MyViewController:.... NSControlTextEditingDelegate 
textfield.delegate = self
//compile error: Cannot assign value of type 'MyViewController' to type 'NSTextFieldDelegate?'

//
class  MyViewController:.... NSTextFieldDelegate 
textfield.delegate = self  //compiles ok

完整示例

//
//  MyViewController.swift
//

import Cocoa

class MyViewController: NSViewController, NSTextFieldDelegate {

    //--------------------------------------------------------------
    // MARK: -   //call in viewDidLoad
    // MARK: -
    //--------------------------------------------------------------
    func connectTONSControlTextEditingDelegate(){

        //controlTextDidChange and controlTextDidEndEditing are in NSControlTextEditingDelegate
        //this is a parent protocol of NSTextFieldDelegate
        //protocol NSTextFieldDelegate : NSControlTextEditingDelegate
        //so  implement NSTextFieldDelegate 
        //class MyViewController:.... NSTextFieldDelegate
        //then connect it here to NSTextFields

        //then override controlTextDidChange OR controlTextDidEndEditing

        self.textField0.delegate = self
        self.textField1.delegate = self

    }


    //--------------------------------------------------------------
    // MARK: -  NSControlTextEditingDelegate
    // MARK: -
    //--------------------------------------------------------------
    //called everytime you type in field
    override func controlTextDidChange(_ notification: Notification) {

        if let textField = notification.object as? NSTextField {
            if textField === textField0{
                logger.info("[controlTextDidChange][textField0]:'\(textField.stringValue)'")

            }
            else if textField === textField1{
                logger.info("[controlTextDidChange][textField1]:'\(textField.stringValue)'")

            }
            else{
                logger.error("[controlTextDidChange][UNHANDLED NSTextField]")
            }
        }
    }

    //User tapped in NSTextfields and then clicked out - text may not have changed
    //this is actually on NSObject in appkit
    //dont need both controlTextDidChange and controlTextDidEndEditing but no harm if you do
    override func controlTextDidEndEditing(_ notification: Notification){
        if let textField = notification.object as? NSTextField {

            if textField === textField0{
                logger.info("[controlTextDidEndEditing][textField0]:'\(textField.stringValue)'")

            }
            else if textField === textField1{
                logger.info("[controlTextDidEndEditing][textField1]:'\(textField.stringValue)'")

            }
            else{
                logger.error("[controlTextDidEndEditing][UNHANDLED NSTextField]")
            }
        }
    }



    //--------------------------------------------------------------
    // MARK: - NSTextFields
    // MARK: -
    //--------------------------------------------------------------
    @IBOutlet weak var textField0: NSTextField!
    @IBOutlet weak var textField1: NSTextField!

    //--------------------------------------------------------------
    // MARK: - viewDidLoad
    // MARK: -
    //--------------------------------------------------------------
    override func viewDidLoad() {
        super.viewDidLoad()

        //---------------------------------------------------------
        //setup text change delegates - see extension above
        connectTONSControlTextEditingDelegate()
        //---------------------------------------------------------
    }
}