Swift显示使用Unresolve Identifier'UIAlertController'

时间:2015-08-15 07:12:44

标签: macos swift

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)
    }

3 个答案:

答案 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