我目前正在使用Swift编写一个OS X应用程序。我的想法是当用户在NSTextField
中输入文本时,运行一个检查值并将其添加到标签的函数。如何在Swift中实现这一点?
我目前正在使用Swift编写一个OS X应用程序。我的想法是当用户在NSTextField
中输入文本时,运行一个检查值并将其添加到标签的函数。如何在Swift中实现这一点?
ViewController
to protocol NSTextDelegate
.ViewController
as Delegate for TextField
.Implement controlTextDidChange
method.
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
}
}
在 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
}
}
}
}
Swift 5和Xcode 10
//TODO: move oin a better place?
extension NSTextField{ func controlTextDidChange(obj: NSNotification){} }
class TracksController:NSViewController,
NSTextFieldDelegate
{
...
}
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)
}
我知道这个问题已经在一段时间前得到了解答,但是我最终在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)")
}
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)
}
//If you want to trim Ex: Email ID, please use the below code.
class ViewController: NSViewController {
@IBOutlet weak var emailTextField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
emailTextField.delegate = self
}
}
extension ViewController : NSTextFieldDelegate {
func controlTextDidBeginEditing(_ obj: Notification) {
print(#function)
}
func controlTextDidEndEditing(_ obj: Notification) {
print(#function)
}
func controlTextDidChange(_ obj: Notification) {
print(#function)
if let textField = obj.object as? NSTextField {
let givenText = textField.stringValue
//Here you add other types of white space
let possibleWhiteSpace: NSArray = [" ", "\t", "\n\r", "\n","\r"]
var string: String = givenText as String
possibleWhiteSpace.enumerateObjects { (whiteSpace, idx, stop) -> Void in
string = string.replacingOccurrences(of: whiteSpace as! String, with: "") as String
}
print(string)
emailTextField.stringValue = string as String
}
}
}
extension String {
func removeAllTypesOfWhiteSpaces(_ inputString : String) -> String {
//Here you add other types of white space
let possibleWhiteSpace: NSArray = [" ","\t", "\n\r", "\n","\r"]
var outputStr: String = inputString as String
possibleWhiteSpace.enumerateObjects { (whiteSpace, idx, stop) -> Void in
outputStr = outputStr.replacingOccurrences(of: whiteSpace as! String, with: "") as String
}
return outputStr
}
}
extension LoginViewController : NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
let givenText = textField.stringValue
let resultStr = givenText.removeAllTypesOfWhiteSpaces(givenText)
textField.stringValue = resultStr as String
}
}
}