[MacOS NSAlert的使用]

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

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

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

如图:
c45b3dbe8e716d64796e5f2183a3afcb

代码如下:

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

如图:
519a70025bac107f94ac548e08f2a26b
代码如下:

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

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

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

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

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

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

如图:
Image

代码如下:

  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.  
  15. }

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

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

如图:
Image
代码如下:

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

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

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

转载于:https://www.cnblogs.com/rayshen/p/4229943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值