将xib UI转换为故事板

时间:2016-12-08 21:57:21

标签: storyboard swift3 macos-sierra

以下是代码:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }

    @IBAction func btn(_ sender: Any) {
        let alert = NSAlert()
        alert.messageText = "Warning"
        alert.informativeText = "Zombies approaching"
        alert.alertStyle = NSAlertStyle.critical
        alert.showsSuppressionButton = true
        alert.suppressionButton!.title = "Stop scaring me"
        alert.addButton(withTitle: "Ignore")
        alert.addButton(withTitle: "Run")
        alert.addButton(withTitle: "Panic")
        alert.addButton(withTitle: "Do nothing")

        let handler = {(choice: NSModalResponse) -> Void in
            switch choice {
            case NSAlertFirstButtonReturn:
                print("Ignore")
            case NSAlertSecondButtonReturn:
                print("Run")
            case NSAlertThirdButtonReturn:
                print("Panic")
            case NSAlertThirdButtonReturn + 1:
                print("Do nothing")
            default:
                break
            }
            if alert.suppressionButton!.state == 1 {
                print("Checked.")
            } else {
                print("Not checked")
            }
        }
        alert.beginSheetModal(for: window, completionHandler: handler)
    }

}

这一切都基于xib。我想知道如何用故事板编写相同的UI?我可以创建一个新的视图控件并使用sheet segue将按钮连接到它,但我会丢失所有现成的NSAlert好东西。

1 个答案:

答案 0 :(得分:0)

要执行此操作,您只需执行以下操作:

  1. 将新的ViewController拖到故事板中
  2. 文件 - >新 - >档案 - > Cocoa Class(Subclass NSViewController)
  3. 将故事板上的ViewController设置为此类(即 SomeViewController,或您在上一步中选择的任何名称)
  4. 将一个按钮拖到Storyboard
  5. 中的ViewController上
  6. Ctrl +将新Button拖到SomeViewController.swift文件中 创建一个动作(不是插座,在弹出框中选择"动作" 窗口)
  7. 将您的提醒代码粘贴到那里,然后按
  8. 进行调用
  9. btn
  10. 中删除AppDelegate功能