Swift: How to continuously send an action from a NSTextField
Set the delegate of the text field to the class which is supposed to receive the action and implement
func controlTextDidChange(_ obj: NSNotification)
This method is invoked when text in a control such as a text field or
form changes. The control posts aNSControlTextDidChangeNotification
notification, and if the control’s delegate implements this method, it
is automatically registered to receive the notification. Use the key
@"NSFieldEditor" to obtain the field editor from the userInfo
dictionary of the notification object.
Programmatically set NSTextField action
You can set it in code. It is actually a property on the text field's cell:
// Same as IB's “Sent On End Editing”.
myTextField.cell?.sendsActionOnEndEditing = true
// Same as IB's “Send on Enter Only”.
myTextField.cell?.sendsActionOnEndEditing = false
Sending in values in a NSTextField: objective c
The button is optional. You can link the TextField
's "Value Changed" event to a function, then updates the Slider
's value with the TextField
's value.
How to update the value of a NSTextField object while the user is editing it?
I figured out how to do this:
extension ViewController: NSTextFieldDelegate {
func controlTextDidChange(_ obj: Notification) {
guard let textView = obj.object as? NSTextField else {
return
}
// codes
}
}
Determine when NSSlider knob is 'let go' in continuous mode
This works for me (and is easier than subclassing NSSlider):
- (IBAction)sizeSliderValueChanged:(id)sender {
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
BOOL startingDrag = event.type == NSLeftMouseDown;
BOOL endingDrag = event.type == NSLeftMouseUp;
BOOL dragging = event.type == NSLeftMouseDragged;
NSAssert(startingDrag || endingDrag || dragging, @"unexpected event type caused slider change: %@", event);
if (startingDrag) {
NSLog(@"slider value started changing");
// do whatever needs to be done when the slider starts changing
}
// do whatever needs to be done for "uncommitted" changes
NSLog(@"slider value: %f", [sender doubleValue]);
if (endingDrag) {
NSLog(@"slider value stopped changing");
// do whatever needs to be done when the slider stops changing
}
}
Select next row when Enter is pressed while editing a NSTextField inside a NSTableView
I would use the NSControlTextEditingDelegate method
- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command
with the text field to detect the enter key and
- (void)editColumn:(NSInteger)columnIndex row:(NSInteger)rowIndex withEvent:(NSEvent *)theEvent select:(BOOL)flag
to select the next row in the table view.
Click event NSTextField OSX
Got it working with that:
1) Create a subclass of NSTextField.
import Cocoa
class MyTextField: NSTextField {
override func mouseDown(theEvent:NSEvent) {
let viewController:ViewController = ViewController()
viewController.textFieldClicked()
}
}
2) With Interface building, select the text field you want to have a focus on. Navigate to Custom Class on the right pane. Then set the class of the text field to the one you have just created.**
3) The following is an example for ViewController.
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func textFieldClicked() -> Void {
print("You've clicked on me!")
}
}
Swift: Call Function when NSTextField is Edited
You will have to make use of NSControlTextEditingDelegate. Connect the NSTextField delegate
to your ViewController if you are using storyboard. If you create it programmatically can just set it in code like this: textfield.delegate = self
in viewDidLoad()
In the NSControlTextEditingDelegate
there is a controlTextDidChange
that you can make use of to get notification when text is edited.
extension ViewController: NSControlTextEditingDelegate {
override func controlTextDidChange(_ notification: Notification) {
if let textField = notification.object as? NSTextField {
print(textField.stringValue)
//do what you need here
}
}
}
Related Topics
Metal: Limit Mtlrendercommandencoder Texture Loading to Only Part of Texture
Bold Words That Start with Letter
What's The How to Access a Swift Package Item from Objective-C
Got Error: Return an Empty String to Bool
How to Keep Nsmenuitem Selected in a Nspopover with Nsmenu
Is It a Good Way to Access Instance Variable with Self? If I Use a Lot
Downloading PDF Using Nsurlsession (Http Post) with Parameters
How to Collect The Return Value of a Function (Swift 3)
Performance Issue with Swiftui List
Use The Same View for Adding and Editing Coredata Objects
Swift Nsbundle Pathforresource() Returns Nil
Return Object for a Method Inside Completion Block
Binary Search: Error in Passing Array
How to Set Local Notifications Between 8Am and 8Pm Every Day
Streaming .M3U8 Using Mpmovieplayercontroller Does Not Work
Calling Closure Inside Closure