如何将文本字段中的数据保存在空数组中

时间:2019-01-03 15:00:32

标签: ios swift uitextfield

让我们谈谈数组,对于创建数组,我没有任何问题,我的问题是客户在文本字段中插入数据,然后转到常用标签,但与此同时,我创建了一个数组我将在pickerView内部的各种控制器中使用它,然后用户将检查Picker并自动插入数据,而不是重写数据。我不知道我是否清楚。

添加视图控制器

import UIKit

class ViewController: UIViewController {

    @IBOutlet var fieldNome: UITextField!

    var studentEdit : ModelArray?
    var arra = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        if let student = studentEdit{

            fieldNome.text = student.nome
        }
    }

    @IBAction func cancel(_ sender: Any) {

        dismiss(animated: true, completion: nil)
    }

    @IBAction func save(_ sender: UIBarButtonItem) {

        if let student = studentEdit{

            guard let newStudent = fieldNome.text else{
                return
            }
            student.nome = fieldNome.text

            DataManager.shared.salva()
            arra.append(newStudent)
            DataManager.shared.lc?.tableView.reloadData()

            dismiss(animated: true, completion: nil)
        }else{

            DataManager.shared.newStudent(nome: fieldNome.text!)

            guard let newStudent = fieldNome.text else{
                return
            }
            arra.append(newStudent)

            dismiss(animated: true, completion: nil)
        }
    }

}

列表控制器需要将保存的文本显示在细胞标签中并使用PICKERVIEW

import UIKit

class ListController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource{

    @IBOutlet weak var picker1: UIPickerView!


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == picker1{
            return arra.count
        }
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == picker1{
            return arra[row]
        }
        return ""
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if pickerView == picker1{
            return labelAss.text = arra[row]

        }
    }

    var arra = [String]()

    @IBOutlet var labelAss: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        picker1.delegate = self
        picker1.dataSource = self

        self.navigationItem.leftBarButtonItem = self.editButtonItem 

        DataManager.shared.lc = self
    }

    // MARK: - Table view data source

    // Quante sezioni?
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return DataManager.shared.storage.count // contiamo gli elementi nell'array
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ArrayCell

        let student = DataManager.shared.storage[indexPath.row]

        cell.labelName.text = student.nome

        return cell
    }


    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true // si
    }


    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            DataManager.shared.storage.remove(at: indexPath.row) 

            tableView.deleteRows(at: [indexPath], with: .fade) 
        }
    }


    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true // si
    }


    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        DataManager.shared.storage.swapAt(fromIndexPath.row, to.row) 
    }
}

如何在必须同时显示标签和pickerView的控制器中实现“ arra”数组?

0 个答案:

没有答案