xcode - xCode 按钮变量干扰
问题描述
我有这个项目,我有两个按钮来创建提醒列表。我有一个输入文本框、一个添加和清除按钮以及一个输出标签。我的添加按钮单独工作正常,但是当我对清除按钮进行编码时,添加按钮停止工作。是因为变量的干扰吗?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var input: UITextField!//input text field
@IBOutlet weak var output: UILabel!//output label
var list: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
output.text = "" //clear the output just in case at the start of the code
}
@IBAction func add(_ sender: Any) {
var tempInput: String = input.text! //create a temporary variable for the input of the string
list.append(tempInput)//add the string to the array
tempInput = ""//empty the array otherwise the first input is printed twice
for i in 0..<list.count{//as long as i is within the list quantity
tempInput += list[i] + "\n"// the backslash n creates a new line
}
output.text = tempInput//set the newly changed input variable that was in an array to the output label
}
@IBAction func clear(_ sender: Any) {
var tempClear: String = ""
list.removeAll()//clear the entire array
for i in 0..<list.count{
list.removeAll()
tempClear = list[i]
}
output.text = tempClear
}
}
解决方案
推荐阅读
- webpack - 开发模式混淆的 webpack 4 配置
- python - 如何根据 dfB 中的列/行值,用另一个数据框 (dfB) 中的值填充 pandas 数据框 (dfA) 列“A”?
- ecmascript-6 - ES6/8 语法 - 箭头函数
- javascript - 无法在 Firebase 函数中连接到 MongoDB
- git - 无需更改本地 dev 分支即可将远程 master 推送到远程 dev 分支的最佳解决方案
- javascript - javascript中的macrotask和microtask谁是第一?
- asp.net-core - 如何在 blazor 服务器应用程序中使用信号器从 http 客户端获取令牌?
- html - Flex Navbar 在 Safari 中不显示,但在 Chrome 中有效
- java - 停车场小程序练习
- r - 按年份预测婴儿名字的最佳预测模型/方法