0
Вот код:Переводит XIb UI в раскадровке
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. Мне интересно, как написать тот же интерфейс с раскадрой? Я мог бы создать новый элемент управления представлением и подключить к нему кнопку с листом, но я потерял бы все готовые лакомства NSAlert.
уже пробовали его раньше. Проблема заключается в 'alert.beginSheetModal (для: window, completeHandler: handler)'. Видите ли, нет «окна»? – qed
Извините, я пропустил это для macOS. Вместо этого попробуйте использовать 'alert.runModal()'. Проверьте https://developer.apple.com/reference/appkit/nsalert/1535441-runmodal – Austin