首页 > 解决方案 > 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
    }
    
    
    
}

标签: xcode

解决方案


推荐阅读