macOS开发中警告视图有两种显示方式:
对话框模式的告警窗口
//对话框模式的告警窗口
NSInteger choice = NSRunAlertPanel(@"title", @"Message", @"@Default", @"Alternate", @"Other");
NSLog(@"Log_choice:%ld", (long)choice);
if (choice == NSAlertDefaultReturn) {//1
}else if(choice == NSAlertAlternateReturn) {//0
}else if(choice == NSAlertOtherReturn) {//-1
}else if(choice == NSAlertErrorReturn) {//-2
}else {
}
sheet模式运行的警告窗口
//sheet模式运行的警告窗口
NSAlert *alert = [NSAlert alertWithMessageText:@"Message" defaultButton:@"Default" alternateButton:@"alternameButton" otherButton:@"otherButton" informativeTextWithFormat:@"%@", @"this is alert message"];
// [alert beginSheetModalForWindow:[NSApp mainWindow] modalDelegate:self didEndSelector:@selector(alertEnded:code:context:) contextInfo:NULL];
[alert beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSModalResponse returnCode) {
if (returnCode == NSModalResponseOK) {
NSLog(@"Log_222");
}else if(returnCode == NSModalResponseCancel) {
NSLog(@"Log_333");
}
}];
若使用代理方式,则需要添加代理方法如下:
- (void)alertEnded:(NSAlert *)alert code:(NSInteger)choice context:(void *)v {
if (choice == NSAlertDefaultReturn) {
}
}