Click Inside Swift 2.2 Osx

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).

  1. click on this view controller
  2. check the right panel and the 3rd icon on the top. Click it
  3. You will see "Custom Class" "Class" "Module"
  4. In "Class", drop down and choose the view controller class you implemented
  5. Make sure you implemented this buttonTap function in the class
  6. In the storyboard make sure you see the button
  7. hold the control key, click on the button and drag to the yellow cube
  8. release your mouse
  9. see the buttonTapped function show in the menu popping up
  10. choose this function


Related Topics



Leave a reply



Submit