user2713376
user2713376

Reputation:

How to show alert pop-up in in Cocoa on macOS?

I want to display a pop-up for displaying information in macOS, similar to a UIAlert or UIAlertController in iOS.

Is their anything in Cocoa similar to UIAlertView in iOS? How can I pop-up an alert in macOS?

Upvotes: 42

Views: 27857

Answers (6)

blakeness
blakeness

Reputation: 261

Swift 5.1

func confirmAbletonIsReady(question: String, text: String) -> Bool {
    let alert = NSAlert()
    alert.messageText = question
    alert.informativeText = text
    alert.alertStyle = NSAlert.Style.warning
    alert.addButton(withTitle: "OK")
    alert.addButton(withTitle: "Cancel")
    return alert.runModal() == NSApplication.ModalResponse.alertFirstButtonReturn
}

Update of @Giang

Upvotes: 19

Maor
Maor

Reputation: 3430

Swift 3.0 Example :

Declaration:

 func showCloseAlert(completion: (Bool) -> Void) {
        let alert = NSAlert()
        alert.messageText = "Warning!"
        alert.informativeText = "Nothing will be saved!"
        alert.alertStyle = NSAlertStyle.warning
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        completion(alert.runModal() == NSAlertFirstButtonReturn)
 }

Usage :

    showCloseAlert { answer in
        if answer {
            self.dismissViewController(self)
        }
    }

Upvotes: 6

Ahmed Khan
Ahmed Khan

Reputation: 85

you can use this method in Swift

 func dialogOKCancel(question: String, text: String) -> Bool
        {
            let alert = NSAlert()
            alert.messageText = question
            alert.informativeText = text
            alert.alertStyle = NSAlertStyle.warning
            alert.addButton(withTitle: "OK")
            alert.addButton(withTitle: "Cancel")
            return alert.runModal() == NSAlertFirstButtonReturn
        }

And then call it in this way

let answer = dialogOKCancel(question: "Ok?", text: "Choose your answer.")

answer will be true or false on selecting "OK" or "Cancel" respectively.

Upvotes: 4

Giang
Giang

Reputation: 2739

Swift 3.0

let alert = NSAlert.init()
alert.messageText = "Hello world"
alert.informativeText = "Information text"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
alert.runModal()

Upvotes: 24

Surjeet Singh
Surjeet Singh

Reputation: 11949

You can use NSAlert in cocoa. This is same as UIAlertView in ios. you can pop-up alert by this

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"Ok" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Alert pop up displayed"];
[alert runModal];

EDIT:

This is the latest used method as above method is deprecated now.

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Message text."];
[alert setInformativeText:@"Informative text."];
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:@"Ok"];
[alert runModal];

Upvotes: 49

Abizern
Abizern

Reputation: 150745

There is the cunningly named NSAlert class which can show a dialog or a sheet to present your alert.

Upvotes: 5

Related Questions