Cocoa有没有简单的输入框?
Is there a simple input box in Cocoa?
Cocoa 中是否有一个内置的、简单的输入框,它是为检索字符串而设计的(就像我记得的很好的 Visual Basic 中一样)?
我想我可以设计一个小的 window 来做到这一点,但我更愿意使用原生的等价物(如果存在这样的东西;如果有的话我找不到它)。
谢谢。
如果您想要一个带有文本字段的对话框,您需要自己创建它或 put an NSTextField
into an NSAlert
(note that the linked answer presents a modal dialog that will block all interaction with the rest of your application; if you don't want that, you need to present it as a sheet on a window)。
感谢 DarkDust 为我指明了正确的方向。我永远不会在 NSAlerts 中搜索 "accessory views"(我缺少正确的术语来欺骗 Google 或 SO 给我货物!)。我也忘了说我用的是Swift,所以我敲了个速译:
func getString(title: String, question: String, defaultValue: String) -> String {
let msg = NSAlert()
msg.addButtonWithTitle("OK") // 1st button
msg.addButtonWithTitle("Cancel") // 2nd button
msg.messageText = title
msg.informativeText = question
let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
txt.stringValue = defaultValue
msg.accessoryView = txt
let response: NSModalResponse = msg.runModal()
if (response == NSAlertFirstButtonReturn) {
return txt.stringValue
} else {
return ""
}
}
更新 Swift 5. 我总是将可重复使用的项目(如提醒)放在应用程序管理器中 class。而且我喜欢将我的闭包作为类型别名,以便更好地组织它们并使参数更清晰。
typealias promptResponseClosure = (_ strResponse:String, _ bResponse:Bool) -> Void
func promptForReply(_ strMsg:String, _ strInformative:String, vc:ViewController, completion:promptResponseClosure) {
let alert: NSAlert = NSAlert()
alert.addButton(withTitle: "OK") // 1st button
alert.addButton(withTitle: "Cancel") // 2nd button
alert.messageText = strMsg
alert.informativeText = strInformative
let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
txt.stringValue = ""
alert.accessoryView = txt
let response: NSApplication.ModalResponse = alert.runModal()
var bResponse = false
if (response == NSApplication.ModalResponse.alertFirstButtonReturn) {
bResponse = true
}
completion(txt.stringValue, bResponse)
}
然后像这样调用它(我的应用程序的 git 管理部分需要这个):
myAppManager.promptForReply("Changes were added to the repo, do you want to commit them?", "If you are commiting, add your commit message below.", vc: self, completion: {(strCommitMsg:String, bResponse:Bool) in
if bResponse {
print(strCommitMsg)
}
})
Cocoa 中是否有一个内置的、简单的输入框,它是为检索字符串而设计的(就像我记得的很好的 Visual Basic 中一样)?
我想我可以设计一个小的 window 来做到这一点,但我更愿意使用原生的等价物(如果存在这样的东西;如果有的话我找不到它)。
谢谢。
如果您想要一个带有文本字段的对话框,您需要自己创建它或 put an NSTextField
into an NSAlert
(note that the linked answer presents a modal dialog that will block all interaction with the rest of your application; if you don't want that, you need to present it as a sheet on a window)。
感谢 DarkDust 为我指明了正确的方向。我永远不会在 NSAlerts 中搜索 "accessory views"(我缺少正确的术语来欺骗 Google 或 SO 给我货物!)。我也忘了说我用的是Swift,所以我敲了个速译:
func getString(title: String, question: String, defaultValue: String) -> String {
let msg = NSAlert()
msg.addButtonWithTitle("OK") // 1st button
msg.addButtonWithTitle("Cancel") // 2nd button
msg.messageText = title
msg.informativeText = question
let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
txt.stringValue = defaultValue
msg.accessoryView = txt
let response: NSModalResponse = msg.runModal()
if (response == NSAlertFirstButtonReturn) {
return txt.stringValue
} else {
return ""
}
}
更新 Swift 5. 我总是将可重复使用的项目(如提醒)放在应用程序管理器中 class。而且我喜欢将我的闭包作为类型别名,以便更好地组织它们并使参数更清晰。
typealias promptResponseClosure = (_ strResponse:String, _ bResponse:Bool) -> Void
func promptForReply(_ strMsg:String, _ strInformative:String, vc:ViewController, completion:promptResponseClosure) {
let alert: NSAlert = NSAlert()
alert.addButton(withTitle: "OK") // 1st button
alert.addButton(withTitle: "Cancel") // 2nd button
alert.messageText = strMsg
alert.informativeText = strInformative
let txt = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
txt.stringValue = ""
alert.accessoryView = txt
let response: NSApplication.ModalResponse = alert.runModal()
var bResponse = false
if (response == NSApplication.ModalResponse.alertFirstButtonReturn) {
bResponse = true
}
completion(txt.stringValue, bResponse)
}
然后像这样调用它(我的应用程序的 git 管理部分需要这个):
myAppManager.promptForReply("Changes were added to the repo, do you want to commit them?", "If you are commiting, add your commit message below.", vc: self, completion: {(strCommitMsg:String, bResponse:Bool) in
if bResponse {
print(strCommitMsg)
}
})