import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var helloButton: NSButton!
@IBAction func showAlert(sender: AnyObject) {
var alert = UIAlertController(title: "Hello!", message: "Hello, world!",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.helloButton.setTitle("Clicked", forState: UIControlState.Normal)
}
答案 0 :(得分:9)
UIAlertController
是一个iOS类,包含在UIKit
中。但是当您正在使用NSViewController
而不是导入UIKit
(而不是Cocoa
)的Mac应用时,您无法使用此课程。
因此,如果您想在Mac应用中创建提醒,请参阅例如How to make an alert controller in mac using swift
答案 1 :(得分:0)
在Mac OS上,您使用NSAlert
代替UIAlertController
。使用NSAlert
的一个很好的例子是in this SO answer。
答案 2 :(得分:0)
在我的情况下,是删除导入Foundation或将导入UIKit移到Foundation上方。
import UIKit
import Foundation //Remove if needed