3
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)
    }
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

3 Answers3

12

UIAlertController is an iOS class, contained in UIKit. But as you're doing a Mac app extending NSViewController and not importing UIKit (instead Cocoa), this class is not available to you.

So if you want to create an alert in your Mac app, see for example How to make an alert controller in mac using swift

Community
  • 1
  • 1
Florian Pfisterer
  • 1,205
  • 12
  • 21
  • The linked solution is wrong. It is suggesting to use UIAlertController, which doesn't exist in Mac. – Houman Jun 10 '21 at 07:50
2

On mac OS you use NSAlert instead of UIAlertController. A nice example of using NSAlert is given in this SO answer.

CodeBrew
  • 6,457
  • 2
  • 43
  • 48
0

In my case was removing import Foundation or moving import UIKit above Foundation.

import UIKit
import Foundation //Remove if needed
Marlhex
  • 1,870
  • 19
  • 27