xcode Cocoa中有没有简单的输入框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28362472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a simple input box in Cocoa?
提问by Marc Fearby
Is there a built-in, simple, input box in Cocoa which is designed for retrieving a string (like there is in good ol' Visual Basic that I remember)?
Cocoa 中是否有一个内置的、简单的、用于检索字符串的输入框(就像我记得的 Visual Basic 中那样)?
I suppose I could design a tiny window to do that but would prefer to use a native equivalent (if such a thing exists; I can't find it if it does).
我想我可以设计一个小窗口来做到这一点,但更喜欢使用本机等效物(如果存在这样的东西;如果存在,我找不到它)。
Thanks.
谢谢。
采纳答案by DarkDust
If you'd like a dialog with a text field, you either need to create it yourself or put an NSTextField
into an NSAlert
(note that the linked answer presents a modaldialog 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).
如果您想要一个带有文本字段的对话框,您要么需要自己创建它,要么将其NSTextField
放入NSAlert
(请注意,链接的答案提供了一个模态对话框,该对话框将阻止与应用程序其余部分的所有交互;如果您不这样做”不希望那样,您需要将其显示为窗口上的工作表)。
回答by Marc Fearby
Thank you DarkDust for pointing me in the right direction. I would never have searched for "accessory views" in NSAlerts (I was lacking the right terms to trick Google or SO into giving me the goods!). I also forgot to mention that I'm using Swift, so I've knocked up a quick translation:
感谢 DarkDust 为我指明了正确的方向。我永远不会在 NSAlerts 中搜索“辅助视图”(我缺乏正确的术语来欺骗 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 ""
}
}
回答by PruitIgoe
Updating for Swift 5. I always put reusable items like alerts in an app manager class. And I like to keep my closures as a typealias to better organize them and keep the arguments cleaner.
更新 Swift 5。我总是在应用程序管理器类中放置可重复使用的项目,如警报。而且我喜欢将我的闭包作为类型别名,以便更好地组织它们并保持参数更清晰。
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)
}
Then call it like so (I needed this for a git management part of my app):
然后像这样调用它(我的应用程序的 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)
}
})