如何添加两个文本框的内容?
How do you add the content of two textfields?
在我的应用程序中,我有两个文本字段、一个标签和一个按钮。我设法让一个文本域的内容显示在标签中,但我想显示两个文本域的内容。
@IBAction func buttonPushed(sender: UIButton) {
Label.text = "\(textfield1.text)"
+ "\(textfield2.text)"
上面的代码是我最接近的代码,但标签显示:
Optional"whatever I typed in the textfield"Optional"whatever I typed
in the textfield"
您是在连接字符串,而不是添加数字。为了添加值,您需要将文本字段值转换为数字,例如:
field.text!.toInt()
!
展开值以删除 "Optional"。 toInt()
将字符串(文本)转换为整数。
最终,您的代码将如下所示:
@IBAction func buttonPushed(sender: UIButton) {
Label.text = "\(textfield1.text!.toInt())"
+ "\(textfield2.text!.toInt())"
您必须打开文本
Label.text = textfield1.text! + textfield2.text!
在我的应用程序中,我有两个文本字段、一个标签和一个按钮。我设法让一个文本域的内容显示在标签中,但我想显示两个文本域的内容。
@IBAction func buttonPushed(sender: UIButton) {
Label.text = "\(textfield1.text)"
+ "\(textfield2.text)"
上面的代码是我最接近的代码,但标签显示:
Optional"whatever I typed in the textfield"Optional"whatever I typed in the textfield"
您是在连接字符串,而不是添加数字。为了添加值,您需要将文本字段值转换为数字,例如:
field.text!.toInt()
!
展开值以删除 "Optional"。 toInt()
将字符串(文本)转换为整数。
最终,您的代码将如下所示:
@IBAction func buttonPushed(sender: UIButton) {
Label.text = "\(textfield1.text!.toInt())"
+ "\(textfield2.text!.toInt())"
您必须打开文本
Label.text = textfield1.text! + textfield2.text!