34

iOS の UIAlert または UIAlertController と同様に、macOS で情報を表示するためのポップアップを表示したいと考えています。

iOS の UIAlertView に似た Cocoa の機能はありますか? macOS でアラートをポップアップ表示するにはどうすればよいですか?

4

6 に答える 6

45

NSAlertココアで使えます。UIAlertViewこれはiosと同じです。これでアラートをポップアップできます

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

編集:

上記の方法は現在廃止されているため、これは最新の使用方法です。

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Message text."];
[alert setInformativeText:@"Informative text."];
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:@"Ok"];
[alert runModal];
于 2013-08-24T10:21:15.830 に答える
21

スイフト3.0

let alert = NSAlert.init()
alert.messageText = "Hello world"
alert.informativeText = "Information text"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
alert.runModal()
于 2016-11-29T10:59:32.590 に答える
5

スウィフト 3.0 の例:

宣言:

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

使用法 :

    showCloseAlert { answer in
        if answer {
            self.dismissViewController(self)
        }
    }
于 2017-07-08T09:16:56.427 に答える
4

ダイアログまたはシートを表示して警告を表示できる、ずる賢い名前のNSAlertクラスがあります。

于 2013-08-24T10:20:53.473 に答える