如何在Cocoa应用程序中实现消息框?
我已经在cocoa应用程序中实现了删除功能,现在我想在用户单击“删除”按钮时显示一个消息框。看一下,它有一个同步方法: 正如Peter提到的,更好的选择是使用窗口上的警报,例如:如何在Cocoa应用程序中实现消息框?,cocoa,Cocoa,我已经在cocoa应用程序中实现了删除功能,现在我想在用户单击“删除”按钮时显示一个消息框。看一下,它有一个同步方法: 正如Peter提到的,更好的选择是使用窗口上的警报,例如: [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
[alert beginSheetModalForWindow:window
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
可通过以下方式添加按钮:
返回代码告诉您按下了哪个按钮:
- (void) alertDidEnd:(NSAlert *)a returnCode:(NSInteger)rc contextInfo:(void *)ci {
switch(rc) {
case NSAlertFirstButtonReturn:
// "First" pressed
break;
case NSAlertSecondButtonReturn:
// "Second" pressed
break;
// ...
}
}
自从得到公认的答案以来,已经过了很长一段时间,事情发生了变化:
- 斯威夫特越来越受欢迎李>
已被弃用,我们应该改用beginSheetModalForWindow(uxAE:modalDelegate:DiEndSelector:contextInfo:)
beginSheetModalForWindow:completionHandler:
func messageBox() {
let alert = NSAlert()
alert.messageText = "Do you want to save the changes you made in the document?"
alert.informativeText = "Your changes will be lost if you don't save them."
alert.addButtonWithTitle("Save")
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Don't Save")
alert.beginSheetModalForWindow(window, completionHandler: savingHandler)
}
func savingHandler(response: NSModalResponse) {
switch(response) {
case NSAlertFirstButtonReturn:
println("Save")
case NSAlertSecondButtonReturn:
println("Cancel")
case NSAlertThirdButtonReturn:
println("Don't Save")
default:
break
}
}
如果需要同步版本,请执行以下操作:
func messageBox() {
let alert = NSAlert()
alert.messageText = "Do you want to save the changes you made in the document?"
alert.informativeText = "Your changes will be lost if you don't save them."
alert.addButtonWithTitle("Save")
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Don't Save")
let result = alert.runModal()
switch(result) {
case NSAlertFirstButtonReturn:
println("Save")
case NSAlertSecondButtonReturn:
println("Cancel")
case NSAlertThirdButtonReturn:
println("Don't Save")
default:
break
}
}
更好的是,在包含“删除”按钮的窗口中,以工作表的形式开始警报。通过这种方式,用户可以继续使用应用程序中的任何其他窗口。哇,它工作得很好。但是如何在此警报中放置更多按钮以及如何获取这些按钮呢events@mik:使用。文档中还有一个可能会对您有所帮助的选项:[alert beginSheetModalForWindow:[NSApp mainWindow]completionHandler:^(NSModalResponse returnCode){if(returnCode==NSAlertFirstButtonReturn){}};你救了我一天!
func messageBox() {
let alert = NSAlert()
alert.messageText = "Do you want to save the changes you made in the document?"
alert.informativeText = "Your changes will be lost if you don't save them."
alert.addButtonWithTitle("Save")
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Don't Save")
alert.beginSheetModalForWindow(window, completionHandler: savingHandler)
}
func savingHandler(response: NSModalResponse) {
switch(response) {
case NSAlertFirstButtonReturn:
println("Save")
case NSAlertSecondButtonReturn:
println("Cancel")
case NSAlertThirdButtonReturn:
println("Don't Save")
default:
break
}
}
func messageBox() {
let alert = NSAlert()
alert.messageText = "Do you want to save the changes you made in the document?"
alert.informativeText = "Your changes will be lost if you don't save them."
alert.addButtonWithTitle("Save")
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Don't Save")
let result = alert.runModal()
switch(result) {
case NSAlertFirstButtonReturn:
println("Save")
case NSAlertSecondButtonReturn:
println("Cancel")
case NSAlertThirdButtonReturn:
println("Don't Save")
default:
break
}
}