NSAlert组件应用总结

简介: NSAlert组件应用总结

一、引言


   在桌面软件开发中,当用户进行非法的操作或有风险的操作时,时长需要弹出警告框来提示用户。在OS X系统上,NSAlert是专门的警告框组件。其提供了简洁的接口供开发者进行使用。


二、NSAlert的简单使用


   使用警告框最简单的使用方式是提示错误信息,错误信息警告只起到提示用户的作用,其只有一个OK按钮,点击后警告框会关闭。示例如下:


- (IBAction)alert:(id)sender {

   NSError * error = [NSError errorWithDomain:@"testError" code:1001 userInfo:@{@"userid":@"1000"}];

   NSAlert * alert = [NSAlert alertWithError:error];

   [alert runModal];

}

效果图如下:

image.png



警告框的展现有两种方式,分别为模态窗与弹出抽屉。弹出抽屉会显示在当前绑定的窗口上,模态窗则会自成窗口,弹出在屏幕中央。


   你也可以对警告框进行自定义设置,例如文本,标题,图标等,示例如下:


- (IBAction)alert:(id)sender {

   NSAlert * alert = [[NSAlert alloc]init];

   alert.icon = [NSImage imageNamed:@"icon"];

   alert.messageText = @"警告信息";

   alert.informativeText = @"额外提供的内容";

   alert.showsHelp = YES;

   alert.helpAnchor = @"mac";

   alert.alertStyle = NSAlertStyleInformational;

   alert.showsSuppressionButton = YES;

   [alert.suppressionButton setTarget:self];

   [alert.suppressionButton setAction:@selector(click)];

   [alert addButtonWithTitle:@"1"];

   [alert addButtonWithTitle:@"2"];

   [alert addButtonWithTitle:@"3"];

   [alert addButtonWithTitle:@"4"];

   long res =  [alert runModal];

   NSLog(@"%ld",res);

}

效果如下:

image.png



三、NSAlert属性与方法解析


NSAlert类中的属性和方法解析如下:


//直接使用错误信息创建警告框

+ (NSAlert *)alertWithError:(NSError *)error;

//设置警告框信息

@property (copy) NSString *messageText;

//设置额外信息内容

@property (copy) NSString *informativeText;

//设置警告框图标

@property (null_resettable, strong) NSImage *icon;

//向警告框中添加按钮

- (NSButton *)addButtonWithTitle:(NSString *)title;

//按钮数组

@property (readonly, copy) NSArray<NSButton *> *buttons;

//是否显示帮助按钮

@property BOOL showsHelp;

//设置帮助手册锚点 用于定于

@property (nullable, copy) NSString *helpAnchor;

//设置警告框风格

/*

typedef NS_ENUM(NSUInteger, NSAlertStyle) {

   NSAlertStyleWarning = 0,     //警告风格

   NSAlertStyleInformational = 1,//提示信息风格

   NSAlertStyleCritical = 2    //验证风格

};

*/

@property NSAlertStyle alertStyle;

//是否显示不再提示按钮

@property BOOL showsSuppressionButton NS_AVAILABLE_MAC(10_5);

//获取不再提示按钮

@property (nullable, readonly, strong) NSButton *suppressionButton NS_AVAILABLE_MAC(10_5);

//代理对象

@property (nullable, weak) id<NSAlertDelegate> delegate;

//以模态窗口的方式弹出警告框,这个方法是同步的,当用户点击警告框中按钮后会返回,返回的NSModalResponse实际上是

//整型数据,第1个按钮为1000,后面一次递增,如1001,1002...

- (NSModalResponse)runModal;

//以窗口抽屉的方式弹出警告框,这个方法是异步的,当用户点击警告框中的按钮后会回调block

- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^ __nullable)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);

NSAlertDelegate协议中只定义了一个方法,如下:


@protocol NSAlertDelegate <NSObject>

@optional

//当用户点击帮助按钮后回调的方法 返回值决定是否弹出帮助窗口

- (BOOL)alertShowHelp:(NSAlert *)alert;

@end

除了上面列出的方法外,NSAlert中还有两个已经弃用的便捷构造和弹出方法,如下:


//创建警告框

+ (NSAlert *)alertWithMessageText:(nullable NSString *)message defaultButton:(nullable NSString *)defaultButton alternateButton:(nullable NSString *)alternateButton otherButton:(nullable NSString *)otherButton informativeTextWithFormat:(NSString *)format, ...;

//弹出警告框

- (void)beginSheetModalForWindow:(NSWindow *)window modalDelegate:(nullable id)delegate didEndSelector:(nullable SEL)didEndSelector contextInfo:(nullable void *)contextInfo;

目录
相关文章
|
数据可视化 TensorFlow 算法框架/工具
TensorBoard
TensorBoard 是一个用于可视化 TensorFlow 图形和训练进度的工具,它可以帮助我们更好地理解训练过程中的数据结构、计算图、损失函数、评估指标等。TensorBoard 可以展示多种信息,例如训练过程中绘制的图像、网络结构、直方图、图片和渲染等。
362 0
|
6月前
|
SQL 前端开发 Java
Spring的三层架构
Spring MVC 三层架构(表现层、业务层、数据访问层)通过职责分离提升代码可维护性与扩展性。表现层(Controller)接收请求并返回响应;业务层(Service)处理核心逻辑与事务;数据访问层(Mapper)负责数据库操作与数据映射,共同实现高效、清晰的系统开发。
537 0
|
存储 iOS开发 Perl
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
1027 2
|
安全 搜索推荐 生物认证
FOFA基础和使用技巧
FOFA基础和使用技巧
|
机器学习/深度学习 存储 自然语言处理
从零开始学习Java神经网络、自然语言处理和语音识别,附详解和简易版GPT,语音识别完整代码示例解析
从零开始学习Java神经网络、自然语言处理和语音识别,附详解和简易版GPT,语音识别完整代码示例解析
445 0
|
人工智能 分布式计算 Java
说说XXLJob分片任务实现原理?
说说XXLJob分片任务实现原理?
2110 0
说说XXLJob分片任务实现原理?
若依如何添加目录菜单,左边的内容,找到左侧目录系统管理下的菜单管理,上级菜单什么意思,创建好放哪里,这里的首页,系统管理,系统监控是主类目是并集 ,显示排序,值越小排名越靠前
若依如何添加目录菜单,左边的内容,找到左侧目录系统管理下的菜单管理,上级菜单什么意思,创建好放哪里,这里的首页,系统管理,系统监控是主类目是并集 ,显示排序,值越小排名越靠前
|
PyTorch 算法框架/工具 并行计算
PyTorch 2.2 中文官方教程(二十)(3)
PyTorch 2.2 中文官方教程(二十)
436 0
|
前端开发 Java Spring
Spring MVC 出现Request method ‘GET‘ not supported解决办法
Spring MVC 出现Request method ‘GET‘ not supported解决办法
|
SQL Oracle 关系型数据库
零基础入门 Oracle数据库:轻松上手
零基础入门 Oracle数据库:轻松上手
386 0