以下是代码:
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好东西。
答案 0 :(得分:0)
要执行此操作,您只需执行以下操作:
btn
AppDelegate
功能
醇>