2016-12-08 4 views
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.

ответ

0

Для этого вам просто нужно сделать следующее:

  1. Перетащите новый ViewController в раскадровке
  2. File -> New -> Файл -> Какао класса (Подкласс NSViewController)
  3. Set ViewController на раскадровку к этому классу (т.е. SomeViewController, или любое имя вы выбрали в предыдущем шаге)
  4. Перетащите кнопку на ViewController в раскадровки
  5. Ctrl + Перетащите новую кнопку в свой файл SomeViewController.swift на . Создайте Action (не выход, выберите «Действие» в popover )
  6. Вставьте свой код предупреждения туда, он будет называться
  7. Удалить функцию btn из вашего AppDelegate
+0

уже пробовали его раньше. Проблема заключается в 'alert.beginSheetModal (для: window, completeHandler: handler)'. Видите ли, нет «окна»? – qed

+0

Извините, я пропустил это для macOS. Вместо этого попробуйте использовать 'alert.runModal()'. Проверьте https://developer.apple.com/reference/appkit/nsalert/1535441-runmodal – Austin

Смежные вопросы