How to catch some events on a NSControl in swift - swift

I am writing an application for OSX in Swift and I am looking for a good way to catch events on a NSControl.
Obviously, I searched but the informations I found are often unclear or old.
In my case, I would like to catch several events on a NSTextField (key up, text changed, focus lost,...).
When I push on “Enter” in the NSTextField, it sends an action. Maybe is there a way to send an action when I click or write in the NSTextField?

You can subclass NSTextField and override textDidChange for text change, textDidEndEditing for lost focus and keyUp method for key up. Try like this:
import Cocoa
class CustomTextField: NSTextField {
override func viewWillMove(toSuperview newSuperview: NSView?) {
// customize your field here
frame = newSuperview?.frame.insetBy(dx: 50, dy: 50) ?? frame
}
override func textDidChange(_ notification: Notification) {
Swift.print("textDidChange")
}
override func textDidEndEditing(_ notification: Notification) {
Swift.print("textDidEndEditing")
}
override func keyUp(with event: NSEvent) {
Swift.print("keyUp")
}
}
View Controller sample Usage:
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let textField = CustomTextField()
view.addSubview(textField)
}
}
Sample

Related

Subclass of NSDatePicker mouseDown event changes datePicker instance

I have instantiated and initialized a subclass of NSDatePicker in AppDelegate -> applicationDidFinishLaunching and overrode mouseDown(with event: NSEvent)in the datePicker subclass. When I press the mouse button in the datePicker and break in its overridden mouseDown func the instance is not the one I instantiated in applicationDidFinishLaunching and so is not initialized.
I've tried creating the instance at different entry points thinking it might be a timing thing but I've gotten nowhere. I'm out of ideas and feeling a little feeble. Any Help?
The datePicker:
import Cocoa
class AlarmIVDatePicker: NSDatePicker {
var viewController: ViewController!
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
return true
}
override func mouseDown(with event: NSEvent) {
let stop = 0
}
}
The ViewController:
class ViewController: NSViewController, NSWindowDelegate{
var alarmIVDatePicker: AlarmIVDatePicker!
override func viewDidLoad() {
super.viewDidLoad()
alarmIVDatePicker = AlarmIVDatePicker()
alarmIVDatePicker.viewController = self
}
I expected I could access the values I had set but the instance is not the one I created and all the values are nil
Okay. Here's what you can do to track this down. Add this code to your AlarmIVDatePicker class:
override init(frame frameRect: NSRect) {
Swift.print("AlarmIVDatePicker being called here")
super.init(frame: frameRect)
}
convenience init() {
self.init(frame: .zero)
}
required init?(coder: NSCoder) {
Swift.print("hey, you DID drop this into a storyboard or XIB file!")
super.init(coder: coder)
}
If you set breakpoints inside these init methods, you'll catch when and where they are being called where you did not expect.

How to detect which NSTextField is clicked in Cocoa?

I have two NSTextField objects which I want to highlight when user clicks on it.
The initial text field is already highlighted on NSWindow load. I am able to get mouse down event for text field click, but unable to distinguish which textfield did the user tapped.
I tried using hitTest on the text field using the NSPoint obtained from the NSEvent object, but the NSView returned is nil. The view it returns is that of the window's view and not that text field.
class SettingsViewController: NSViewController {
private var sview: SettingsView?
override func viewDidLoad() {
initEvents()
}
override func loadView() {
if let settingsView = SettingsView.createFromNib() {
self.view = settingsView
self.sview = settingsView as? SettingsView
}
}
func initEvents() {
self.sview!.emailTextField.delegate = self
}
}
extension SettingsViewController: NSTextFieldDelegate, NSTextDelegate {
override func mouseDown(with event: NSEvent) {
self.log.debug("mouse down: \(event.buttonNumber), \(event.eventNumber), \(event.locationInWindow)")
// How to know which text field triggered this?
}
func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
self.log.debug("control delegate")
return false
}
func textField(_ textField: NSTextField, textView: NSTextView, shouldSelectCandidateAt index: Int) -> Bool {
self.log.debug("text field should select")
return true
}
func textShouldBeginEditing(_ textObject: NSText) -> Bool {
self.log.debug("text field should being editing")
return true
}
}
class SettingsView: NSView {
private let log = Logger()
private static var topLevelObjects: NSArray?
#IBOutlet weak var emailTextField: ASTextField!
#IBOutlet weak var passwordTextField: NSSecureTextField!
// ...
}
I am adding delegate to only one text field.
self.sview!.emailTextField.delegate = self
But when I click on the passwordTextField, I am getting the mouse click event as well. Why is this happening?
How to distinguish NSTextField mouse click and highlight the text field?
I tried subclassing NSTextField and adding click handler, but it is not working.
class ASTextField: NSTextField {
private let log = Logger()
required init?(coder: NSCoder) {
super.init(coder: coder)
bootstrap()
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
bootstrap()
}
override func awakeFromNib() {
super.awakeFromNib()
bootstrap()
}
func bootstrap() {
self.delegate = self
}
}
extension ASTextField: NSTextFieldDelegate {
override func mouseDown(with event: NSEvent) {
// This is not working
self.log.debug("mouse down")
super.mouseDown(with: event)
}
}
If what you're looking for is to be able to select the text when you click (focus) the text field, you can override the class to simplify your task and you won't have to worry about locating the clicked field from the delegate.
For an NSView object, when it gets focus (ie. clicking or tabbing) it will call becomeFirstResponder so we can hook in there.
When an NSTextField becomes editable (or selectable) it grabs a reusable 'field editor' and overlays it on top of your text field during the editing. If your NSTextField has focus, you can grab this field editor using the currentEditor() call on the view.
So, once you have the field editor, you can perform selectAll on the editor to select the text.
Example class :-
class AutoselectOnFocusTextField: NSTextField {
override func becomeFirstResponder() -> Bool {
guard super.becomeFirstResponder() else {
return false
}
if let editor = self.currentEditor() {
editor.perform(#selector(selectAll(_:)), with: self, afterDelay: 0)
}
return true
}
}
Hope this helps!
I updated the ASTextField as below.
class ASTextField: NSTextField {
// ...
override func mouseDown(with event: NSEvent) {
self.sendAction(#selector(didClick(_:)), to: self)
super.mouseDown(with: event)
}
#objc func didClick(_ event: NSEvent) {
self.log.debug("did click")
}
}
In the SettingsView, I missed calling super.layout(), without which the click won't work, nor the other text field will get focus when clicked.
class SettingsView: NSView {
// ...
override func layout() {
self.log.debug("layout method")
super.layout() // This is important
}
}
NSTextField delegate methods are not required.
The method you overwrote, mouseDown(with), isn't a member of the NSTextFieldDelegate or NSTextDelegate protocols. You overwrote NSViewController.mouseDown(with).
Whenever that method is called, the thing that was clicked is your SettingsViewController's view.
To react to your textfield being selected, you use NSTextFieldDelegate .textField(_:textView:shouldSelectCandidateAt:), which you already have. The value of the textView parameter is the text view that was selected.

How do I check when a NSTextView changes if I have two text views?

In a macOS app with two NSTextView, I am trying to check when a textView changes, so far I have done this:
class ViewController: NSViewController, NSTextViewDelegate {
#IBOutlet var mainTextField: NSTextView!
#IBOutlet var findPanelFindTextView: NSTextView!
func textDidChange(_ notification: Notification) {
print("Hello!") // works only with mainTextField
}
override func viewDidLoad() {
super.viewDidLoad()
mainTextField.delegate = self // for textDidChange
findPanelFindTextField.delegate = self // for textDidChange
}
}
Only the first NSTextView (mainTextField) triggers the method textDidChange.
I already see this question Check if NSTextView has been edited and that implementation works for my first textView but not for my second textView.
Besides the fact that you wrote findPanelFindTextField but it should be findPanelFindTextView, you should check the object that posted the notification, create a textView object from it but cast it from Any to NSTextView and then create a switch to check which textview is posting the notification:
#IBOutlet var mainTextView: NSTextView!
#IBOutlet var findPanelFindTextView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
mainTextView.delegate = self
findPanelFindTextView.delegate = self
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
switch textView {
case mainTextView:
print("mainTextView changed")
case findPanelFindTextView:
print("findPanelFindTextView changed")
default:
break
}
}

Why is windowDidMove() not being called? [duplicate]

I'm trying to know when a window closes, I implemented this code:
class ViewController: NSViewController, NSWindowDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let window: NSWindow? = view.window
window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}
Unfortunately nothing happens, what could I made wrong?
Documents: https://developer.apple.com/documentation/appkit/nswindow/1419400-willclosenotification
PS
I already read this question without to find a solution: Handle close event of the window in Swift
The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:
class ViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}

Trying to know when a window closes in a macOS Document based application

I'm trying to know when a window closes, I implemented this code:
class ViewController: NSViewController, NSWindowDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let window: NSWindow? = view.window
window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}
Unfortunately nothing happens, what could I made wrong?
Documents: https://developer.apple.com/documentation/appkit/nswindow/1419400-willclosenotification
PS
I already read this question without to find a solution: Handle close event of the window in Swift
The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:
class ViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.delegate = self
}
func windowWillClose(_ aNotification: Notification) {
print("windowWillClose")
}
}