[MacOS NSAlert的使用]

时间:2023-03-09 17:02:22
[MacOS NSAlert的使用]

源:http://helloitworks.com/863.html

NSAlert用于弹出一个确认对话框,在程序中被广泛地使用。常见的场景是用户删除数据,会弹出对话框给用户确认,免得用户不小心导致了误操作。

NSAlert可以采用Modal Window的方式展示

如图:
[MacOS NSAlert的使用]

代码如下:

  1. //采用Modal Window的方式展示
  2. - (IBAction)ShowNSAlertWindow:(id)sender
  3. {
  4. NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
  5. defaultButton:@"defaultButton"
  6. alternateButton:@"alternateButton"
  7. otherButton:@"otherButton"
  8. informativeTextWithFormat:@"informativeText"];
  9. NSUInteger action = [alert runModal];
  10. //响应window的按钮事件
  11. if(action == NSAlertDefaultReturn)
  12. {
  13. NSLog(@"defaultButton clicked!");
  14. }
  15. else if(action == NSAlertAlternateReturn )
  16. {
  17. NSLog(@"alternateButton clicked!");
  18. }
  19. else if(action == NSAlertOtherReturn)
  20. {
  21. NSLog(@"otherButton clicked!");
  22. }
  23. }

NSAlert也可以采用Sheet的方式展示

如图:
[MacOS NSAlert的使用]
代码如下:

  1. //采用Sheet的方式展示
  2. - (IBAction)ShowNSAlertSheet:(id)sender
  3. {
  4. NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
  5. [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
  6. NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
  7. defaultButton:@"defaultButton"
  8. alternateButton:@"alternateButton"
  9. otherButton:@"otherButton"
  10. informativeTextWithFormat:@"informativeText"];
  11. //__bridge_retained for arc
  12. [alert beginSheetModalForWindow:self.window
  13. modalDelegate:self
  14. didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
  15. contextInfo:(__bridge void *)(extrasDict )];
  16. }
  17. //响应Sheet的按钮事件
  18. - (void)alertSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
  19. {
  20. if (returnCode == NSAlertDefaultReturn)
  21. {
  22. NSLog(@"alternateButton clicked!");
  23. //show you how to use contextInfo
  24. //__bridge_transfer for arc
  25. NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
  26. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  27. }
  28. else if(returnCode == NSAlertAlternateReturn )
  29. {
  30. NSLog(@"alternateButton clicked!");
  31. }
  32. else if(returnCode == NSAlertOtherReturn)
  33. {
  34. NSLog(@"otherButton clicked!");
  35. }
  36. }

源代码:https://github.com/helloitworks/NSAlert

=====================华丽的分割线=====================

可以说NSAlert是标准的,中规中矩,几乎可以应用到所有需要提示框的地方。但我们很难通过继承的方式来扩展NSAlert的功能,事实上NSAlert的设计初衷就是提供一个提示框标准,并不希望用户通过继承去自定义。
在特定的应用程序中,我们经常希望可以自己提供一个自定义窗口,并可以像NSAlert那样采用Modal Window的方式或者采用Sheet的方式来展示。比如黑色主题的程序希望这个NSAlert窗口是黑色的,而不是标准的灰白色,这样才显得和谐。

下面我通过继承NSObject的方式来实现一个SYXAlert类,SYXAlert类采用一个自定义的窗口SYXAlert来模拟NSAlert。

SYXAlert可以采用Modal Window的方式展示

如图:
[MacOS NSAlert的使用]

代码如下:

  1. //采用Window的方式展示
  2. - (IBAction)ShowSYXAlertWindow:(id)sender
  3. {
  4. SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertWindow" okButton:@"Ok" cancelButton:@"Cancel"];
  5. NSInteger action = [alert runModal];
  6. if(action == SYXAlertOkReturn)
  7. {
  8. NSLog(@"SYXAlertOkButton clicked!");
  9. }
  10. else if(action == SYXAlertCancelReturn )
  11. {
  12. NSLog(@"SYXAlertCancelButton clicked!");
  13. }
  14. }

注:modal对话框窗口左上角是没有Close、Minimize、Resize这些按钮的,所以在xib中去掉这些按钮

SYXAlert也可以采用Sheet的方式展示

如图:
[MacOS NSAlert的使用]
代码如下:

  1. //采用Sheet的方式展示
  2. - (IBAction)ShowSYXAlertSheet:(id)sender
  3. {
  4. NSMutableDictionary * extrasDict = [[NSMutableDictionary alloc] init];
  5. [extrasDict setObject:@"http://www.baidu.com" forKey:@"link"];
  6. SYXAlert *alert = [SYXAlert alertWithMessageText:@"SYXAlertSheet" okButton:@"Ok" cancelButton:@"Cancel"];
  7. [alert beginSheetModalForWindow:self.window
  8. modalDelegate:self
  9. didEndSelector:@selector(alertSheetDidEnd:returnCode:contextInfo:)
  10. contextInfo:(__bridge void*)extrasDict];
  11. }
  12. //响应Sheet的按钮事件
  13. - (void)alertSheetDidEnd:(NSAlert *)alert
  14. returnCode:(NSInteger)returnCode
  15. contextInfo:(void *)contextInfo {
  16. if (returnCode == SYXAlertOkReturn)
  17. {
  18. NSLog(@"SYXAlertOkButton clicked!");
  19. //show you how to use contextInfo
  20. //__bridge_transfer for arc
  21. NSString *url = [(__bridge NSDictionary*)contextInfo objectForKey:@"link"];
  22. [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
  23. }
  24. else if(returnCode == SYXAlertCancelReturn )
  25. {
  26. NSLog(@"SYXAlertCancelButton clicked!");
  27. }
  28. }

注:xib的window属性有一个选项,就是visible at launch,默认是勾选,窗口无法采用sheet的方式附在父窗口上;勾掉,窗口才能采用sheet的方式附在父窗口上

源代码:https://github.com/helloitworks/SYXAlert