参考文章
虽然部分API已经停止使用,但是作者总结的还是相当详细的,值得一看。
* To display an informative message
* To take confirmation from user before continuing with a transaction
* As a success or failure response to the end user.
* Giving the user more than one option to take a action (save, dont-save, cancel etc).
* An NSAlert object displays an application model or as a sheet attached to a document window.
上代码
- (IBAction)showAlert:(NSButton *)sender {
NSAlert * alert = [[NSAlert alloc]init];
alert.messageText = @"This is messageText";
alert.alertStyle = NSAlertStyleInformational;
[alert addButtonWithTitle:@"continue"];
[alert addButtonWithTitle:@"cancle"];
[alert setInformativeText:@"NSWarningAlertStyle \r Do you want to continue with delete of selected records"];
[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
NSLog(@"xxx");
if (returnCode == NSModalResponseOK){
NSLog(@"(returnCode == NSOKButton)");
}else if (returnCode == NSModalResponseCancel){
NSLog(@"(returnCode == NSCancelButton)");
}else if(returnCode == NSAlertFirstButtonReturn){
NSLog(@"if (returnCode == NSAlertFirstButtonReturn)");
}else if (returnCode == NSAlertSecondButtonReturn){
NSLog(@"else if (returnCode == NSAlertSecondButtonReturn)");
}else if (returnCode == NSAlertThirdButtonReturn){
NSLog(@"else if (returnCode == NSAlertThirdButtonReturn)");
}else{
NSLog(@"All Other return code %ld",(long)returnCode);
}
}];
}
多个按钮的Alert
- (IBAction)showDifferentAlert:(NSButton *)sender {
NSAlert * alert = [[NSAlert alloc]init];
alert.messageText = @"This is messageText";
alert.alertStyle = NSAlertStyleInformational;
[alert addButtonWithTitle:@"Button One"]; //will generate a return code of 1000
[alert addButtonWithTitle:@"Button Two"]; //will generate a return code of 1001
[alert addButtonWithTitle:@"Button Three"]; //will generate a return code of 1002
[alert addButtonWithTitle:@"Button Four"]; //will generate a return code of 1003
[alert addButtonWithTitle:@"Button Five"]; //will generate a return code of 1004
[alert setInformativeText:@"NSWarningAlertStyle \r Do you want to continue with delete of selected records"];
[alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
if (returnCode == NSAlertFirstButtonReturn) {
NSLog(@"(returnCode == NSAlertFirstButtonReturn)");
}else if(returnCode == NSAlertSecondButtonReturn){
NSLog(@"(returnCode == NSAlertSecondButtonReturn)");
}else if(returnCode == NSAlertThirdButtonReturn){
NSLog(@"(returnCode == NSAlertThirdButtonReturn)");
}else if (returnCode == NSAlertThirdButtonReturn){
NSLog(@"All Other return code %ld",(long)returnCode);
}else{
NSLog(@"All Other return code %ld",(long)returnCode);
}
}];
}
相对来说还是很简单的,相对UIAlertViewController还是很相似的。