如何从Swift中的AppDelegate打开一个具有自己的ViewController的新窗口

5q4ezhmt  于 2022-12-17  发布在  Swift
关注(0)|答案(4)|浏览(198)

我制作了一个带有下拉菜单的状态栏应用程序。我想从下拉菜单中打开一个设置窗口。我制作了一个带有自己的ViewController的设置窗口。
问题是我不知道如何示例化和显示我所做的设置窗口。我试图跟踪互联网上的每一个线程,但没有任何成功。
我的视图控制器:

class SettingsViewController: NSViewController {
   @IBOutlet var ipAddress: NSTextField!
   @IBOutlet var port: NSTextField!

   @IBAction func connect(sender: AnyObject) {}
   override func viewDidLoad() {
      super.viewDidLoad()
   }
}

我的应用程序代理:

class AppDelegate: NSObject, NSApplicationDelegate {
   @IBOutlet var statusMenu: NSMenu!
   var statusItem: NSStatusItem?
   var tcpService: TcpService = TcpService()

   func applicationDidFinishLaunching(aNotification: NSNotification?) {

      let bar = NSStatusBar.systemStatusBar()

      statusItem = bar.statusItemWithLength(20)
      statusItem!.menu = statusMenu
      statusItem!.image = NSImage(byReferencingFile: NSBundle.mainBundle().pathForResource("16*16", ofType: "png"))
      statusItem!.highlightMode = true

      tcpService.initOutputStream("192.168.1.1", Port: 8888)
   }

   func applicationWillTerminate(aNotification: NSNotification?) {
      // Insert code here to tear down your application
   }
   @IBAction func openSettings(sender: AnyObject) {
      // open settings for ip and port optional port
   }
}
vhipe2zx

vhipe2zx1#

在Swift 3:

var myWindow: NSWindow? = nil
        let storyboard = NSStoryboard(name: "Main",bundle: nil)
        let controller: EditorViewController = storyboard.instantiateController(withIdentifier: "editorViewController") as! ViewController
        myWindow = NSWindow(contentViewController: controller)
        myWindow?.makeKeyAndOrderFront(self)
        let vc = NSWindowController(window: myWindow)
        vc.showWindow(self)
envsm3lx

envsm3lx2#

2022年
1.在普通主情节提要中,点击以添加新窗口控制器。

1.精确地点击红色“X”,然后点击蓝色圆圈,然后在绿色条目处输入“ExampleID”。
1.在应用程序普通主视图控制器中,添加以下内容
变量:

var otherWindow: NSWindowController?

功能:

private func otherWindow() {
    let sb = NSStoryboard(name: "Main", bundle: nil)
    otherWindow = sb.instantiateController(
       withIdentifier: "ExampleID") as! NSWindowController
    otherWindow?.showWindow(self)
}

就是这样。
想打电话就打给otherWindow
问题:
不可避免地,你会想以某种方式设置otherWindow,例如,透明,或者其他。不幸的是,这本身就是一个完整的主题,但你可以这样做:

private func otherWindow() {
    ... as above ...
    otherWindow?.window?.ExampleSetup()
}

然后

extension NSWindow {
    func ExampleSetup() {
        self.styleMask = .borderless
        self.collectionBehavior = [.fullScreenPrimary]
        self.level = .floating
        self.isMovable = false
        self.titleVisibility = .hidden
        // etc etc etc ..
        guard let screen = self.screen ?? NSScreen.main else {
            print("what the???")
            return
        }
        self.setFrame(screen.frame, display: true)
        // consider also .visibleFrame
    }
}
6ojccjat

6ojccjat3#

enum Storyboards: String {
case main = "Main"

func instantiateVC<T>(_ identifier: T.Type) -> T?  {
    let storyboard = NSStoryboard(name: rawValue, bundle: nil)
    guard let viewcontroller = storyboard.instantiateController(withIdentifier: String(describing: identifier)) as? T else { return nil}
    return viewcontroller
   }
}

var ssoLoginController: IDSSOLoginViewController?
var myWindow: NSWindow? = nil

ssoLoginController = Storyboards.main.instantiateVC(IDSSOLoginViewController.self)
myWindow = NSWindow(contentViewController: ssoLoginController!)
myWindow?.makeKeyAndOrderFront(self)
let vc = NSWindowController(window: myWindow)
vc.showWindow(self)
bsxbgnwa

bsxbgnwa4#

我不是100%完全理解您的问题,但是假设您使用的是故事板(如果您是从头开始,则应该使用),在applicationDidFinishLaunching方法中添加几行代码将有所帮助:

var myWindow: NSWindow? = nil
        let storyboard = NSStoryboard(name: "Main",bundle: nil)
        let controller: SettingsViewController = storyboard?.instantiateControllerWithIdentifier("SettingsViewController") as SettingsViewController
        myWindow = controller.window
        myWindow?.makeKeyAndOrderFront(self)

不要忘记在IB中设置情节提要ID(在上面的示例中为SettingsViewController)!

相关问题