How to test NSTextField - Swift OS X

I am currently creating an OS X application written in Swift. What I want to do is when the user enters text in NSTextField, I want to run a function that checks the value and adds it to the shortcut. How can I do this fast?

+5
source share
5 answers
  • Complies with the ViewControllerprotocol NSTextDelegate.
  • Assign ViewControlleras delegate to TextField.
  • Implement the method controlTextDidChange.

    import Cocoa
    
    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate {
    
        @IBOutlet weak var window: NSWindow!
        @IBOutlet weak var textField: NSTextField!
        @IBOutlet weak var label: NSTextField!
    
    
        func applicationDidFinishLaunching(aNotification: NSNotification)
        {
            textField.delegate = self
        }
    
        override func controlTextDidChange(notification: NSNotification)
        {
            let object = notification.object as! NSTextField
            self.label.stringValue = object.stringValue
        }
    
        func applicationWillTerminate(aNotification: NSNotification) {
            // Insert code here to tear down your application
        }
    }
    

in ViewController:

import Cocoa

class ViewController: NSViewController, NSTextDelegate {

   @IBOutlet weak var label: NSTextField!
   @IBOutlet weak var label2: NSTextField!
   @IBOutlet weak var textField: NSTextField!
   @IBOutlet weak var textField2: NSTextField!

   override func viewDidLoad() {
      super.viewDidLoad()
   }

   override func controlTextDidChange(notification: NSNotification) {
      if let txtFld = notification.object as? NSTextField {
         switch txtFld.tag {
         case 201:
            self.label.stringValue = txtFld.stringValue
         case 202:
            self.label2.stringValue = txtFld.stringValue
         default:
            break
         }
      }
   }
}
+7
source

, - , macOS Swift 3 ( , Swift 4), , .

: -

NSTextFieldDelegate

viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(textDidChange(_:)), name: Notification.Name.NSTextViewDidChangeSelection, object: nil)

:

    func textDidChange(_ notification: Notification) {
        print("Its come here textDidChange")
        guard (notification.object as? NSTextView) != nil else { return }
        let numberOfCharatersInTextfield: Int = textFieldCell.accessibilityNumberOfCharacters()
        print("numberOfCharatersInTextfield = \(numberOfCharatersInTextfield)")
}

, .

+1

Swift 4,

Swift 4

, NSTextfield. , , , , controlTextDidChange: - Objective -C, swift. NSTextFieldDelegate target-c - , swift.

extension NSTextField{ func controlTextDidChange(obj: NSNotification){} }

class SomeViewController:NSViewController,NSTextFieldDelegate {

override func controlTextDidChange(_ obj: Notification)
{
    let object = obj.object as! NSTextField
    let value = object.stringValue
    print(value)
}

, NSTextFieldDelegate, extension.

0

swift 5 xcode 10

//TODO: move oin a better place?
extension NSTextField{ func controlTextDidChange(obj: NSNotification){} }


    class TracksController:NSViewController,
        NSTextFieldDelegate,
        NSSearchFieldDelegate
    {
...
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Search bar:
        self.searchBar.delegate = self

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(controlTextDidChange(_:)),
                                               name: NSTextView.didChangeSelectionNotification,
                                               object: nil)

    }

//MARK: NSTextFieldDelegate

func controlTextDidChange(_ obj: Notification)
{
    guard let object = obj.object as? NSTextView else{
        return
    }


    let delegate = object.delegate
    guard  let searchField = delegate as? NSSearchField else {
        return
    }

    let text = searchField.stringValue
    print(text)

}
0

You can use NSNotificationCenter to monitor the text box.

@IBOutlet weak var textField: UITextField!

In viewDidLoad ()

let noti = NSNotificationCenter.defaultCenter()    
noti.addObserver(self, selector: "textDidChange:", name: UITextFieldTextDidChangeNotification, object: textField)

And then define a function, for example:

func textDidChange(noti: NSNotification) {
    print(textField.text)
}

Now each key element that you enter in the text field will be printed in the textDidChange () function.

Hope for this help

-5
source

Source: https://habr.com/ru/post/1623560/


All Articles