Swift: How can I open a new window clicking a butt

2019-02-15 11:08发布

I'm new with this programming language and I'd like to create an application that opens a window with some information when I click a button but I don't know how to do that.

I'm not working with storyboards because I read that for professional programming these doesn't work.

I don't want for iOS, I'd like it to be for OS X.

Regards to all!

1条回答
Lonely孤独者°
2楼-- · 2019-02-15 11:39

Thats pretty simple. You can do as follow:

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)
    }
}
查看更多
登录 后发表回答