使用 Swift 将输入从 TextField 打印到 Xcode 中的标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27083943/
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
Printing input from TextField to a Label in Xcode with Swift
提问by Dylan Bailey
I'm working on a simple guessing game app, just to get more comfortable with Swift and Xcode. I have been able to input within userInput and get it to print a message to the console, however when I try to get it to print my input to usersGuess(which is a label), I can not figure it out.
我正在开发一个简单的猜谜游戏应用程序,只是为了更好地使用 Swift 和 Xcode。我已经能够在 userInput 中输入并让它向控制台打印一条消息,但是当我尝试让它将我的输入打印到 usersGuess(这是一个标签)时,我无法弄清楚。
Here's my code within a single view application via Xcode:
这是我通过 Xcode 在单个视图应用程序中的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var correctAnswerLabel: UILabel!
@IBOutlet weak var usersGuess: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
@IBAction func userInput(sender: UITextField) {
println("This is working")
}
}
I'm sure this is simple, but I am scratching my head lol.
我确定这很简单,但我正在挠头哈哈。
回答by mbo42
@IBAction func userInput(sender: UITextField) {
println("This is working")
usersGuess.text = sender.text
}
回答by erichoco
Although I am still new to iOS dev and Swift, I think you could also take a look at the use of delegate in this tutorialApple provides. I guess it might be the code didn't resign your text field's first-responder status. Hence, the usersGuess
could not update. (Anyone who knows how this work please leave a comment.)
虽然我对 iOS 开发和 Swift 还是个新手,但我想你也可以看看Apple 提供的本教程中委托的使用。我想这可能是代码没有放弃您的文本字段的第一响应者状态。因此,usersGuess
无法更新。(谁知道这是如何工作的,请发表评论。)
To do this, basically
要做到这一点,基本上
- Create an outlet for the
UITextField
that receives user's input, say,usersInput
. - Set
ViewController
as a delegate ofusersInput
, which will - Resign the first-responder status of
usersInput
when the Return button on the keyboard is pressed. - Update the text of
usersGuess
.
- 创建一个用于
UITextField
接收用户输入的插座,例如,usersInput
。 - 设置
ViewController
为 的代表usersInput
,这将 usersInput
当按下键盘上的返回按钮时,放弃第一响应者状态。- 更新 的文本
usersGuess
。
Code here:
代码在这里:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var correctAnswerLabel: UILabel!
@IBOutlet weak var usersGuess: UILabel!
@IBOutlet weak var usersInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Set ViewController as a delegate
usersInput.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Here are the callback functions for usersInput
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
usersGuess.text = textField.text
}
@IBAction func buttonPressed() {
correctAnswerLabel.text = "Changes when the button is pressed."
}
@IBAction func userInput(sender: UITextField) {
println("This is working")
}
}