我有在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];
Run Code Online (Sandbox Code Playgroud) 我花了相当多的实验来澄清对Objective-C的"ModalForWindow"语言的一些混淆,以及随后如何使用模态会话.也许以下提示可以节省一些时间:
(如果您不熟悉这个概念:当一个窗口,通常是一个面板,运行模态时,它会阻止应用程序的其他部分响应,直到它被解除.)
"ModalForWindow"在不同的情况下意味着不同的东西.如果您使用loadNibNamed显示由xib定义的面板并且您希望它运行modal,请在显示后调用它:
// Make panelReviewImports modal, so that no other part of app will respond.
[[NSApplication sharedApplication] runModalForWindow:self.panelReviewImports];
Run Code Online (Sandbox Code Playgroud)
并在其解雇方法中采取以下措施:
[[NSApplication sharedApplication] stopModal];
Run Code Online (Sandbox Code Playgroud)
但对于NSAlert,beginSheetModalForWindow中的"窗口"指的是警报将作为工作表附加到的窗口,该窗口将被冻结,直到警报被解除.但该应用程序不会被冻结; 所有其他窗户将保持可操作性.如果您要附加警告如纸和也 明确冻结的应用程序的其余部分,按照beginSheet代码用一个简单的调用runModal并使用返回代码,如下所示:
[alert beginSheetModalForWindow:self.window
modalDelegate:self didEndSelector:@selector(abandonmentAlertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
NSInteger returnCode = [alert runModal];
[self abandonmentAlertDidEnd:alert returnCode:returnCode contextInfo:nil];
Run Code Online (Sandbox Code Playgroud)
(当然,您将实现了abandonmentAlertDidEnd:returnCode:contextInfo:代码作为类方法.)
或者,如果您希望警报作为居中面板运行,请自行调用runModal.
假设您要运行面板模式,如果用户提交无效条目,则后跟警报.在显示警报之前,您必须先停止模式 - 之后,由于某种原因,另一次调用runModalForWindow无法正常工作.对于此场景,您需要一个模态会话:
1)将NSModalSession属性添加到控制器类,因为modalSession必须可以跨多个方法访问.
2)显示面板后,调用beginModalSessionForWindow来实例化modalSession:
self.modalSession = [[NSApplication sharedApplication] beginModalSessionForWindow:self.panelForInput];
Run Code Online (Sandbox Code Playgroud)
3)使用调用runModalSession的while循环进行跟进,当它的返回值不等于NSRunContinuesResponse时断开:
while ([[NSApplication sharedApplication] runModalSession:self.modalSession] == NSRunContinuesResponse)
continue;
Run Code Online (Sandbox Code Playgroud)
循环将中断,当用户点击其中一个面板按钮时,应用程序将释放.(在面板的文本字段中输入将使模态会话保持不变.)
4)在按钮处理中,如果用户的条目无效,则使用runModal调用警报.
5)紧接警报调用之后,在解除警报后将执行的代码中,您将上面使用的相同的while循环置于其中.小组的模态会话重新开始.
6)在关闭面板的处理中,无论是在有效输入还是取消时,都会调用endModalSession,奇怪的是,这是不够的; 你也必须调用stopModal,即使你从未调用runModalForWindow.
[[NSApplication sharedApplication] endModalSession:self.modalSession];
[[NSApplication sharedApplication] stopModal];
[self.panelForInput close];
Run Code Online (Sandbox Code Playgroud) 该应用程序打开一个对话框,NSOpenPanel允许用户选择图像.当我选择图像并选择"打开"时,我收到以下错误:
2016-11-03 10:23:25.589283 PA位置数据[9008:265214] [布局]检测到NSTextField的缺失约束:0x6000001e3a00.它无法放置,因为没有足够的约束来完全定义大小和原点.添加缺少的约束,或设置translatesAutoresizingMaskIntoConstraints = YES,并为您生成约束.如果此视图在macOS 10.12及更高版本上手动布局,您可以选择不从覆盖中调用[super layout].在DETECTED_MISSING_CONSTRAINTS上设置断点以进行调试.此错误仅记录一次.
公开对话然后冻结,不能被解雇.
正如所建议的那样,我为DETECTED_MISSING_CONSTRAINTS设置了一个断点,并DETECTED_MISSING_CONSTRAINTS在此代码中发现执行正在暂停:
func happyAlert(message: String, info: String) {
let myPopup: NSAlert = NSAlert()
myPopup.messageText = message
myPopup.informativeText = info
myPopup.alertStyle = NSAlertStyle.informational
myPopup.addButton(withTitle: "OK")
myPopup.runModal()
}
Run Code Online (Sandbox Code Playgroud)
此警报通知用户图像已通过或未通过各种验证检查.
过去发生的事情是图像被选中,被myPopup.runModal()解雇和警报出现没有任何问题.
现在我得到了关于NSTextField的约束的错误,但是我不明白它们为什么会在这里涉及,特别是因为xCode没有标记任何与底层视图的自动布局问题.
任何人都可以解释可能发生的事情和/或进一步调试的策略吗?我的经历有限,我很困惑.
我想做这样的事情NSAlert:
如您所见,"返回"按钮是第二个按钮.我怎样才能做到这一点?
这是我用来创建我的代码的示例NSAlert,但第一个按钮获得焦点:
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Are you sure you want to disconnect?"];
[alert addButtonWithTitle:@"Disconnect"];
[alert addButtonWithTitle:@"Cancel"];
[alert runModal];
Run Code Online (Sandbox Code Playgroud)
我想关注"取消"按钮.有任何想法吗?谢谢!
我有一个NSAlert项目,它使用NSTextField作为附件项来提供类似提示的对话框.我遇到的唯一问题是,当运行警报时,文本字段不会聚焦.我试图寻找一种方法来改变NSAlert的第一响应者但却一无所获.有没有办法轻松关注NSAlert中的配件项目?
我在我的应用程序中创建了主窗口以进行这些设置:
[self setLevel:kCGDesktopWindowLevel + 1];
[self setCollectionBehavior:
(NSWindowCollectionBehaviorCanJoinAllSpaces |
NSWindowCollectionBehaviorStationary |
NSWindowCollectionBehaviorIgnoresCycle)];
Run Code Online (Sandbox Code Playgroud)
这是一个非常自定义的窗口,可以在桌面上方浮动.
另外,它是一个菜单栏应用程序(LSUIElement).
好吧,所以如果出现问题,我需要显示警报.我是这样做的:
NSAlert *alert = [NSAlert alertWithMessageText:@""
defaultButton:@""
alternateButton:@""
otherButton:@""
informativeTextWithFormat:@""];
[alert runModal];
Run Code Online (Sandbox Code Playgroud)
我当然填写了按钮和其他文字.
这是我的问题:当我的应用程序当前不是关键应用程序,并且弹出此警报时,它不是关键窗口.像这样:

看看窗口是如何选择的?有没有办法改变我的整个应用程序窗口级别?谢谢!
我有一个Cocoa应用程序,它使用NSAlert该类显示应用程序模式警报.我希望警报窗口浮动在所有其他应用程序的窗口之上.可以这样做NSAlert,还是我需要实现自己的窗口?
我不知道这是否重要,但应用程序是一个代理应用程序(LSUIElement是真的)实现为NSStatusItem.(有关该应用程序的更多信息,包括源代码,请查看<here>.)
以下是显示警报的代码:
- (void)showTimerExpiredAlert {
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setMessageText:NSLocalizedString(@"Menubar Countdown Complete", @"Expiration message")];
[alert setInformativeText:NSLocalizedString(@"The countdown timer has reached 00:00:00.",
@"Expiration information")];
[alert addButtonWithTitle:NSLocalizedString(@"OK", @"OK button title")];
[alert addButtonWithTitle:NSLocalizedString(@"Restart Countdown...", @"Restart button title")];
NSInteger clickedButton = [alert runModal];
[alert release];
if (clickedButton == NSAlertSecondButtonReturn) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
我在runModal电话会议前试过这个:
[[alert window] setFloatingPanel:YES];
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
[[alert window] setLevel:NSFloatingWindowLevel];
Run Code Online (Sandbox Code Playgroud)
但是如果我点击另一个应用程序的窗口,这些都不会使窗口保持在其他窗口之上.我怀疑runModal …
我试图将一个归因字符串放在NSTextField中,它本身就在NSAlert里面这是我的代码:
NSTextField *label1 = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 23, 50, 20)];
[label1 setEditable:FALSE];
[label1 setAllowsEditingTextAttributes:TRUE];
[label1 setBezeled:FALSE];
label1.backgroundColor = [NSColor clearColor];
NSString *login = @"Username";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:login];
NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
[attrString beginEditing];
NSRange ran = NSMakeRange(0, 8);
[attrString addAttribute:NSFontAttributeName
value:boldFontName
range:ran];
[attrString endEditing];
NSLog(@"%@",attrString);
[label1 setAttributedStringValue:attrString];
[alert setAccessoryView:label1];
[alert runModal];
Run Code Online (Sandbox Code Playgroud)
但是,一旦调出[alert runModal],我的应用程序就会崩溃
"[__NSCFConstantString pointSize]: unrecognized selector sent to instance 0x7fff74035bb0"
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会这样.它似乎与字符串有关,因为只要我删除[alert setAccessoryView:label1]或给label1一个标准的nsstring,它就可以正常工作.请帮忙!
是否可以创建完全自定义的警报?我现在正在使用自定义工作表,但我希望具有工作表阻止的功能(如-[NSAlert runModal]).
当然,我只是想改变背景,真的和文字颜色.
编辑:我的问题不是很清楚,现在我对其进行了编辑,以明确我需要打开在线网页而不是帮助手册。
\n\n我想在 macOS 项目的 NSAlert 中包含一个问号按钮,该按钮指向带有帮助资源的在线网页。
\n\n我在这里看到有两种可能性:
\n\n\n\n\nvar showHelp: Bool 指定警报是否有帮助按钮。
\n\nvar helpAnchor:字符串?Alert\xe2\x80\x99s HTML 帮助锚点。
\n
但我不知道如何实现它。
\n\n我使用这段代码:
\n\n@IBAction func buttonPressed(_ sender: Any) {\n let myAlert: NSAlert = NSAlert()\n\n myAlert.messageText = "Message"\n myAlert.informativeText = "Informative text."\n\n myAlert.showsSuppressionButton = true\n\n myAlert.addButton(withTitle: "Later")\n myAlert.addButton(withTitle: "Now")\n myAlert.addButton(withTitle: "OK")\n\n let choice = myAlert.runModal()\n\n switch choice {\n case NSAlertFirstButtonReturn:\n print ("OK")\n case NSAlertSecondButtonReturn:\n print ("Now")\n case NSAlertThirdButtonReturn:\n print ("Later")\n default: break\n\n }\n if myAlert.suppressionButton!.state == …Run Code Online (Sandbox Code Playgroud) nsalert ×10
cocoa ×7
objective-c ×6
macos ×5
swift ×2
autolayout ×1
dialog ×1
modal-dialog ×1
nspanel ×1
nstextfield ×1
prompt ×1