Click inside swift 2.2 OSX
You need to set the NSButton
's action
. For example:
let button = NSButton()
button.target = self
button.action = #selector(someAction)
From NSButton Class Reference:
NSButton is a subclass of the NSControl class. An NSButton object
sends an action message to a target object, such as a view controller,
when the button is clicked.
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!")
}
}
Close button event on OS X app swift 2.2
func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool {
NSApplication.sharedApplication().terminate(self)
return true
}
Inside appdeligate work for me.
For Swift 4.2
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
NSApplication.shared.terminate(self)
return true
}
How to show Menu on button press instead of right mouse click in os x
I got it working finally using this code:
@IBOutlet var meeenu: NSMenu!
@IBAction func Options(sender: NSButtonCell) {
meeenu.popUpMenuPositioningItem(meeenu.itemAtIndex(0), atLocation: NSEvent.mouseLocation(), inView: nil)
}
When clicking on an app icon in the dock after it's opened, what event can I receive?
Implement
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool)
In your AppDelegate and make your application visible there.
See the documentation on more implementation details
Xcode way to print Hello World when button is clicked in OS X Swift application?
The way to link a button for OSX is the same for iOS
@IBAction func buttonTapped(button: NSButton)
{
...
}
One thing to note is here we use NSButton.
In the storyboard, you should see a view controller (a yellow cube).
- click on this view controller
- check the right panel and the 3rd icon on the top. Click it
- You will see "Custom Class" "Class" "Module"
- In "Class", drop down and choose the view controller class you implemented
- Make sure you implemented this buttonTap function in the class
- In the storyboard make sure you see the button
- hold the control key, click on the button and drag to the yellow cube
- release your mouse
- see the buttonTapped function show in the menu popping up
- choose this function
Related Topics
Get Bogus Value When Execute Break Point in a Variable
How to Draw a Line Between Two Points in Scenekit
An Issue with Cashapelayer and Uibezierpath Drawing Circle Counterclockwise
How to Save the Attributed String (Text) into File (Swift, Cocoa)
Why Is an Object Not Being Decoded
Using Delegates on Generic Protocol
No Code Signature Found After Pod Installed in Xcode 8
Store Encodables in a Swift Dictionary
Uitextview Font to Always Be Fixed Size
New Firebase Retrieve Data and Put on the Tableview (Swift)
How to Make Swiftui Listmenu with Different Behaviors
How to Get Coreml in Pure Playground Files
How to Use Println in Swift to Format Number
Macos Command Line Tool with Swift Cocoa Framework: Library Not Loaded