Swift: How to open a new window by clicking a button?
I am new to this programming language and I would like to create an application that opens a window with some information when I click a button, but I do not know how to do this.
I don't work with storyboard because I read that for professional programming they don't work.
I don't want it for iOS, I would like it for OS X.
About all!
+3
source to share
1 answer
It's pretty straightforward. You can do the following:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
let myNewWindow = NSWindow(contentRect: NSMakeRect(0,0,640,480), styleMask: NSBorderlessWindowMask, backing: NSBackingStoreType.Buffered, defer: false)
// NSBorderlessWindowMask = 0,
// NSTitledWindowMask = 1 << 0,
// NSClosableWindowMask = 1 << 1,
// NSMiniaturizableWindowMask = 1 << 2,
// NSResizableWindowMask = 1 << 3,
// NSTexturedBackgroundWindowMask = 1 << 8
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
@IBAction func btnNewWindow(sender: AnyObject) {
myNewWindow.opaque = false
myNewWindow.movableByWindowBackground = true
myNewWindow.backgroundColor = NSColor(hue: 0, saturation: 1, brightness: 0, alpha: 0.7)
myNewWindow.makeKeyAndOrderFront(nil)
}
}
+3
source to share