首页 > 其他分享 >Mac开发基础25-NSAlert

Mac开发基础25-NSAlert

时间:2024-08-06 18:16:36浏览次数:7  
标签:25 对话框 NSAlert alert Mac window message 警告

NSAlert 是 macOS 应用中的一个重要控件,用于显示警告与通知对话框。NSAlert 允许开发者创建和配置弹出窗口,用于通知用户、确认操作或显示错误信息。

基本使用

创建和显示简单的警告框

Objective-C

#import <Cocoa/Cocoa.h>

// 实例化 NSAlert
NSAlert *alert = [[NSAlert alloc] init];

// 设置警告标题
[alert setMessageText:@"警告"];

// 设置详细信息
[alert setInformativeText:@"这是一个警告对话框"];

// 添加按钮
[alert addButtonWithTitle:@"确定"];
[alert addButtonWithTitle:@"取消"];

// 显示同步模式对话框 (阻塞,等待用户响应)
NSModalResponse response = [alert runModal];
if (response == NSAlertFirstButtonReturn) {
    NSLog(@"用户点击了确定");
} else if (response == NSAlertSecondButtonReturn) {
    NSLog(@"用户点击了取消");
}

Swift

import Cocoa

// 实例化 NSAlert
let alert = NSAlert()

// 设置警告标题
alert.messageText = "警告"

// 设置详细信息
alert.informativeText = "这是一个警告对话框"

// 添加按钮
alert.addButton(withTitle: "确定")
alert.addButton(withTitle: "取消")

// 显示同步模式对话框 (阻塞,等待用户响应)
let response = alert.runModal()
if response == .alertFirstButtonReturn {
    print("用户点击了确定")
} else if response == .alertSecondButtonReturn {
    print("用户点击了取消")
}

配置警告类型和样式

Objective-C

// 设置警告类型 (例如:NSAlertStyleWarning,NSAlertStyleInformational,NSAlertStyleCritical)
[alert setAlertStyle:NSAlertStyleWarning];

Swift

// 设置警告类型 (例如:.warning,.informational,.critical)
alert.alertStyle = .warning

显示异步模式对话框

Objective-C

// 显示异步模式对话框,使用 completionHandler 来处理用户响应
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
    if (returnCode == NSAlertFirstButtonReturn) {
        NSLog(@"用户点击了确定");
    } else if (returnCode == NSAlertSecondButtonReturn) {
        NSLog(@"用户点击了取消");
    }
}];

Swift

// 显示异步模式对话框,使用 completionHandler 来处理用户响应
alert.beginSheetModal(for: self.window!) { response in
    if response == .alertFirstButtonReturn {
        print("用户点击了确定")
    } else if response == .alertSecondButtonReturn {
        print("用户点击了取消")
    }
}

高级用法

为警告对话框添加自定义视图

Objective-C

// 创建一个自定义视图 (例如,包含一个 NSTextField)
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[textField setStringValue:@"请输入你的名字"];

// 添加自定义视图到警告对话框
[alert setAccessoryView:textField];

Swift

// 创建一个自定义视图 (例如,包含一个 NSTextField)
let textField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
textField.stringValue = "请输入你的名字"

// 添加自定义视图到警告对话框
alert.accessoryView = textField

禁用按钮的自动布局

默认情况下,NSAlert 会为按钮设置自动布局。对于更复杂的布局,可以禁用自动布局。

Objective-C

// 禁用按钮的自动布局
[alert setShowsSuppressionButton:YES];
[alert.suppressionButton setTitle:@"不再显示此消息"];

Swift

// 禁用按钮的自动布局
alert.showsSuppressionButton = true
alert.suppressionButton?.title = "不再显示此消息"

响应抑制按钮点击

NSAlert 可以包含一个抑制按钮(Suppression Button),用于抑制将来的显示。

Objective-C

// 在 completionHandler 中处理抑制按钮点击
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
    if ([alert suppressionButton].state == NSControlStateValueOn) {
        NSLog(@"用户选择不再显示此消息");
    }
}];

Swift

// 在 completionHandler 中处理抑制按钮点击
alert.beginSheetModal(for: self.window!) { response in
    if alert.suppressionButton?.state == .on {
        print("用户选择不再显示此消息")
    }
}

创建不同样式的警告对话框

Objective-C

// 设置对话框为信息提示样式
[alert setAlertStyle:NSAlertStyleInformational];

// 设置对话框为警告样式
[alert setAlertStyle:NSAlertStyleWarning];

// 设置对话框为严重错误样式
[alert setAlertStyle:NSAlertStyleCritical];

Swift

// 设置对话框为信息提示样式
alert.alertStyle = .informational

// 设置对话框为警告样式
alert.alertStyle = .warning

// 设置对话框为严重错误样式
alert.alertStyle = .critical

封装工具类

为了更方便地使用 NSAlert,可以封装一个工具类,提供常见功能的高层接口。

Objective-C

#import <Cocoa/Cocoa.h>

@interface NSAlertHelper : NSObject

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message inWindow:(NSWindow *)window;
+ (void)showCustomAlertWithTitle:(NSString *)title message:(NSString *)message accessoryView:(NSView *)accessoryView inWindow:(NSWindow *)window;
+ (void)showAsyncAlertWithTitle:(NSString *)title message:(NSString *)message inWindow:(NSWindow *)window completionHandler:(void (^)(NSModalResponse))completionHandler;

@end

@implementation NSAlertHelper

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message inWindow:(NSWindow *)window {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:title];
    [alert setInformativeText:message];
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];
    [alert beginSheetModalForWindow:window completionHandler:nil];
}

+ (void)showCustomAlertWithTitle:(NSString *)title message:(NSString *)message accessoryView:(NSView *)accessoryView inWindow:(NSWindow *)window {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:title];
    [alert setInformativeText:message];
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];
    [alert setAccessoryView:accessoryView];
    [alert beginSheetModalForWindow:window completionHandler:nil];
}

+ (void)showAsyncAlertWithTitle:(NSString *)title message:(NSString *)message inWindow:(NSWindow *)window completionHandler:(void (^)(NSModalResponse))completionHandler {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:title];
    [alert setInformativeText:message];
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];
    [alert beginSheetModalForWindow:window completionHandler:completionHandler];
}

@end

Swift

import Cocoa

class NSAlertHelper {
    
    // 显示简单的警告对话框
    static func showAlert(title: String, message: String, inWindow window: NSWindow) {
        let alert = NSAlert()
        alert.messageText = title
        alert.informativeText = message
        alert.addButton(withTitle: "确定")
        alert.addButton(withTitle: "取消")
        alert.beginSheetModal(for: window, completionHandler: nil)
    }
    
    // 显示带有自定义视图的警告对话框
    static func showCustomAlert(title: String, message: String, accessoryView: NSView, inWindow window: NSWindow) {
        let alert = NSAlert()
        alert.messageText = title
        alert.informativeText = message
        alert.accessoryView = accessoryView
        alert.addButton(withTitle: "确定")
        alert.addButton(withTitle: "取消")
        alert.beginSheetModal(for: window, completionHandler: nil)
    }
    
    // 显示异步警告对话框
    static func showAsyncAlert(title: String, message: String, inWindow window: NSWindow, completionHandler: @escaping (NSApplication.ModalResponse) -> Void) {
        let alert = NSAlert()
        alert.messageText = title
        alert.informativeText = message
        alert.addButton(withTitle: "确定")
        alert.addButton(withTitle: "取消")
        alert.beginSheetModal(for: window, completionHandler: completionHandler)
    }
}

使用示例

Objective-C

// 使用示例:显示简单的警告对话框
[NSAlertHelper showAlertWithTitle:@"警告" message:@"这是一个警告对话框" inWindow:self.window];

// 使用示例:显示带有自定义视图的警告对话框
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[textField setStringValue:@"请输入你的名字"];
[NSAlertHelper showCustomAlertWithTitle:@"输入" message:@"请输入信息" accessoryView:textField inWindow:self.window];

// 使用示例:显示异步警告对话框
[NSAlertHelper showAsyncAlertWithTitle:@"异步警告" message:@"这是一个异步警告对话框" inWindow:self.window completionHandler:^(NSModalResponse returnCode) {
    if (returnCode == NSAlertFirstButtonReturn) {
        NSLog(@"用户点击了确定");
    } else if (returnCode == NSAlertSecondButtonReturn) {
        NSLog(@"用户点击了取消");
    }
}];

Swift

// 使用示例:显示简单的警告对话框
NSAlertHelper.showAlert(title: "警告", message: "这是一个警告对话框", inWindow: self.window!)

// 使用示例:显示带有自定义视图的警告对话框
let textField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
textField.stringValue = "请输入你的名字"
NSAlertHelper.showCustomAlert(title: "输入", message: "请输入信息", accessoryView: textField, inWindow: self.window!)

// 使用示例:显示异步警告对话框
NSAlertHelper.showAsyncAlert(title: "异步警告", message: "这是一个异步警告对话框", inWindow: self.window!) { response in
    if response == .alertFirstButtonReturn {
        print("用户点击了确定")
    } else if response == .alertSecondButtonReturn {
        print("用户点击了取消")
    }
}

总结

通过了解 NSAlert 的基本使用、配置警告类型和样式、显示异步模式对话框、添加自定义视图、响应抑制按钮点击以及创建不同样式的警告对话框等高级用法,并封装工具类,你将能够更高效地使用 NSAlert 创建复杂的提示系统。在实际应用中,合理使用这些技巧可以显著提升用户界面的灵活性和用户体验。

标签:25,对话框,NSAlert,alert,Mac,window,message,警告
From: https://www.cnblogs.com/chglog/p/18345774

相关文章

  • Mac开发基础22-NSTabView
    NSTabView是macOS应用中的一个重要控件,用于创建带有多个选项卡的界面,类似于网页浏览器的选项卡功能。它能够将多个视图容器合并到一个控件中,每个视图容器都可以通过选项卡来切换。基本使用创建和初始化Objective-C#import<Cocoa/Cocoa.h>//创建一个NSTabView实例NST......
  • Mac开发基础23-NSMenu
    NSMenu是macOS应用中的一个重要控件,用于创建应用程序的菜单。这些菜单通常出现在屏幕顶部的菜单栏中,但也可以作为上下文菜单出现。NSMenu和NSMenuItem协同工作,NSMenu是菜单容器,而NSMenuItem是菜单项。本指南将详细介绍NSMenu的常见API和基础技巧。基本使用创建和初......
  • Mac开发基础21-NSSplitView
    NSSplitView是macOS应用中的一个重要控件,允许用户调整窗口中的各个子视图大小。它通常用于创建可调整大小的面板布局,例如侧边栏和主内容区域。在本指南中,我们将详细介绍NSSplitView的常见API和基础技巧,并深入探讨相关知识。基本使用创建和初始化Objective-C#import<Co......
  • Mac开发基础20-NSCollectionView
    NSCollectionView是macOS开发中的一种强大控件,类似于iOS上的UICollectionView,用于展示和管理网格、列表等多种布局的数据展示视图。1.基本使用创建和初始化Objective-C#import<Cocoa/Cocoa.h>//创建并初始化一个NSCollectionView实例NSCollectionView*collecti......
  • Mac开发基础19-NSTableView(二)
    进阶使用和技巧1.单击和双击行事件处理Objective-C//单击行时的处理-(void)tableView:(NSTableView*)tableViewdidClickTableColumn:(NSTableColumn*)tableColumn{NSIntegerclickedRow=[tableViewclickedRow];if(clickedRow>=0){NSLog(@"Si......
  • SSM高校学生学业预警系统5253u 本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表系统内容:课程,班级,教师,学生,课程信息,选课信息,考勤信息,成绩信息,学籍信息,预警信息开题报告内容一、选题背景随着高等教育规模的不断扩大和教育质量的提升......
  • Mac开发基础18-NSTableView(一)
    NSTableView是macOS应用程序中用于显示和管理数据表格的控件。它提供了丰富的API和高度自定义的能力,使得开发者可以精细地控制表格的显示和行为。本文将详细介绍NSTableView的常见API和一些基础技巧,并深入探讨其相关知识。1.基本使用创建和初始化Objective-C#import......
  • LlamaCoder:一款开源的平替 Claude Artifacts 项目
    LlamaCoder是一个开源项目,旨在提供一种替代ClaudeArtifacts的解决方案。ClaudeArtifacts是一个商业软件,可能包含一些专有技术或特定的功能集,而LlamaCoder则致力于提供类似的功能,但以开源的形式,允许更广泛的社区参与和贡献。由于LlamaCoder是一个假想的开源项目,我将......
  • Mac开发基础16-NSButton(一)
    NSButton是macOS应用中常用的控件之一,用于处理各种按钮操作。它不仅提供了丰富的API来定制按钮的外观和行为,还可以通过不同的配置实现多种类型的按钮,如push按钮、toggle按钮、radio按钮等。1.基本用法创建和初始化Objective-C//创建和初始化一个NSButton实例NSB......
  • Mac开发基础17-NSButton(二)
    NSButton是一个功能强大且灵活多样的控件,除了基本使用和常见API外,还有一些进阶用法和技巧可以提高按钮的可用性和实现细节。在以下内容中,我会详细介绍一些进阶使用技巧,并封装一个常用的工具类来实现自定义的多种按钮类型。进阶使用和技巧1.自定义按钮的外观和行为Objective-C......