Swift: How can I open a new window clicking a button?

こ雲淡風輕ζ 提交于 2019-12-30 07:02:15

问题


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:


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


来源:https://stackoverflow.com/questions/27893796/swift-how-can-i-open-a-new-window-clicking-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!