iOS の UIAlert または UIAlertController と同様に、macOS で情報を表示するためのポップアップを表示したいと考えています。
iOS の UIAlertView に似た Cocoa の機能はありますか? macOS でアラートをポップアップ表示するにはどうすればよいですか?
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];
スイフト3.0
let alert = NSAlert.init()
alert.messageText = "Hello world"
alert.informativeText = "Information text"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
alert.runModal()
スウィフト 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)
}
}
ダイアログまたはシートを表示して警告を表示できる、ずる賢い名前のNSAlertクラスがあります。