【发布时间】:2015-06-08 14:53:12
【问题描述】:
我有在 Objective-C 中创建和 NSAlert 的代码,但我现在想在 Swift 中创建它。
警报是为了确认用户想要删除文档。
我希望“删除”按钮运行删除功能,而“取消”按钮只是为了解除警报。
我怎样才能用 Swift 写这个?
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Delete"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Delete the document?"];
[alert setInformativeText:@"Are you sure you would like to delete the document?"];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
【问题讨论】:
-
您可能想考虑
beginSheetModal(for:completionHandler:)不已被弃用,实际上它可能是处理模态对话框(在一个块中)的更理想的方式。它也更接近于didEndSelector的旧方式,并且不会停止整个应用程序。