学习 Swift & Xcode - @IBAction func 在 UITextField 为空时重置显示
Learning Swift & Xcode - @IBAction func reset for display if UITextField is empty
免责声明:我正在自学 Swift & Xcode 所以我的问题很简单。
我正在构建一个简单的应用程序来开始,它有一个连接到字符串输出的文本字段。
我正在上的课有一段摘录,内容如下:
"The reset method simply needs to clear out the text of both the nameField and the lyricsView—you can do this by setting each of their text properties to an empty string."
我知道这可能涉及到 if 语句,但我认为对此的解释很差。
这是viewcontroller:
class ViewController: UIViewController {
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var lyricsView: UITextView!
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 reset(_ sender: Any) {
}
@IBAction func displayLyrics(_ sender: Any) {
}
}
有人可以通过将 nameField 和 lyricsView 的属性设置为空字符串以便重置来解释它们的含义吗?
谢谢!
如果要清除textField 或textView 的文本,只需将text
属性 设置为空字符串即可。正如你的课程提示:
The reset method simply needs to clear out the text of both the
nameField and the lyricsView—you can do this by setting each of their
text properties to an empty string.
重置方法应该是这样的:
@IBAction func reset(_ sender: Any) {
nameField.text = ""
lyricsView.text = ""
}
将此添加到重置方法以删除内容:-
nameField.text = ""
lyricsView.text = ""
当您在 nameField
中输入内容时,您的 lyricsView
中会显示一些内容。因此,应该有一种方法可以清除您输入的内容(以及显示的内容)。因此,重置功能(我猜它绑定到一个按钮)。
点击重置后,nameField
和 lyricsView
中的文本应该会消失。您可以通过将两者分配给一个名为 空字符串 的东西来做到这一点,它只是两个双引号:
let anEmptyString = ""
您需要将 ""
分配给 nameField
和 lyricsView
文本 属性。
要清除我将使用的那些字段:
@IBAction func reset(_ sender: Any) {
nameField?.text = ""
lyricsView?.text = ""
}
问号将安全地执行代码,即使由于某些原因,这些字段尚未加载,或已被释放或删除。
免责声明:我正在自学 Swift & Xcode 所以我的问题很简单。
我正在构建一个简单的应用程序来开始,它有一个连接到字符串输出的文本字段。
我正在上的课有一段摘录,内容如下:
"The reset method simply needs to clear out the text of both the nameField and the lyricsView—you can do this by setting each of their text properties to an empty string."
我知道这可能涉及到 if 语句,但我认为对此的解释很差。
这是viewcontroller:
class ViewController: UIViewController {
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var lyricsView: UITextView!
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 reset(_ sender: Any) {
}
@IBAction func displayLyrics(_ sender: Any) {
}
}
有人可以通过将 nameField 和 lyricsView 的属性设置为空字符串以便重置来解释它们的含义吗?
谢谢!
如果要清除textField 或textView 的文本,只需将text
属性 设置为空字符串即可。正如你的课程提示:
The reset method simply needs to clear out the text of both the nameField and the lyricsView—you can do this by setting each of their text properties to an empty string.
重置方法应该是这样的:
@IBAction func reset(_ sender: Any) {
nameField.text = ""
lyricsView.text = ""
}
将此添加到重置方法以删除内容:-
nameField.text = ""
lyricsView.text = ""
当您在 nameField
中输入内容时,您的 lyricsView
中会显示一些内容。因此,应该有一种方法可以清除您输入的内容(以及显示的内容)。因此,重置功能(我猜它绑定到一个按钮)。
点击重置后,nameField
和 lyricsView
中的文本应该会消失。您可以通过将两者分配给一个名为 空字符串 的东西来做到这一点,它只是两个双引号:
let anEmptyString = ""
您需要将 ""
分配给 nameField
和 lyricsView
文本 属性。
要清除我将使用的那些字段:
@IBAction func reset(_ sender: Any) {
nameField?.text = ""
lyricsView?.text = ""
}
问号将安全地执行代码,即使由于某些原因,这些字段尚未加载,或已被释放或删除。