本文介绍了将数据从 tableviewcontroller 快速传递到另一个 tableviewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表单

此表单填充了用户输入的文本字段.回答完所有问题后,会弹出一个按钮以进行保存.

this form gets filled with textfields the user inputs. After answering all the questions a button pops up to save.

我在使这个 tableviewcontroller 将数据传递给新的 tableviewcontroller 时遇到问题.我被卡住了,不知道该怎么做.

I am having a problem making this tableviewcontroller to pass the data to a new tableviewcontroller. I'm stuck and not sure how to go about this.

import UIKit

class TableViewController: UITableViewController, UITextFieldDelegate {

    @IBOutlet weak var saveBtn: UIButton!

    @IBOutlet var firstNameField: UITextField!
    @IBOutlet var middleNameField: UITextField!
    @IBOutlet weak var lastNameField: UITextField!
    @IBOutlet weak var addressField: UITextField!
    @IBOutlet weak var aptNumField: UITextField!
    @IBOutlet weak var cityField: UITextField!
    @IBOutlet weak var stateField: UITextField!
    @IBOutlet weak var zipField: UITextField!
    @IBOutlet weak var phoneOneField: UITextField!
    @IBOutlet weak var phoneTwoField: UITextField!
    @IBOutlet weak var allergiesField: UITextField!
    @IBOutlet weak var DobField: UILabel!
    @IBOutlet weak var sexField: UILabel!
    @IBOutlet weak var hospitalField: UITextField!
    @IBOutlet weak var doctorField: UITextField!

  override func viewDidLoad() {
        super.viewDidLoad()

        //Notifications to push datepicker
        NotificationCenter.default.addObserver(forName: .saveDateTime, object: nil, queue: OperationQueue.main) { (notification) in
            let dateVc = notification.object as! DatePopupViewController
            self.DobField.text = dateVc.formattedDate
        }

        //Notifications to push genderpicker
        NotificationCenter.default.addObserver(forName: .saveGender, object: nil, queue: OperationQueue.main) { (notification) in
            let genderVc = notification.object as! GenderPopupViewController
            self.sexField.text = genderVc.selectedGender
        }

                updateWidthsForLabels(labels: labels)
    }

    //Save Button Function
    func textFieldDidChange(_ textField: UITextField) {
        if textField == firstNameField || textField == lastNameField || textField == middleNameField || textField == addressField || textField == lastNameField || textField == cityField || textField == cityField || textField == stateField || textField == zipField || textField == phoneOneField || textField == phoneTwoField || textField == allergiesField {

            saveBtn.isHidden = true
        } else {
            saveBtn.isHidden = false
        }
    }

  @IBAction func saveBtnPressed(_ sender: Any) {


        performSegue(withIdentifier: "saveFirstPageSegue", sender: self)

}

}

推荐答案

开始创建模型怎么样:

Form.swift

struct Form {
    var firstname: String?
    var middlename: String?
   ....
    var doctor: String?

    init(firstname: String, middlename: String, ..., doctor: String) {
        self.firstname = firstname
        self.middlename = middlename
        ...
        self.doctor = doctor
    }

}

现在您可以在保存数据并将数据推送到新 VC 时创建此表单实例:

now you can create this form instance when saving and pushing the data to the new VC:

yourCurrentForm.swift

yourCurrentForm.swift

@IBAction func saveBtnPressed(_ sender: Any) {
    let formData = Form(firstname: firstNameField.text, middlename: middleNameField.text, ..., doctor: doctorField.text)
    let newVC = myNewViewController()
    newVC.form = formData
    self.navigationController?.pushViewController(newVC, animated: true)
}

NewViewController.swift

NewViewController.swift

class myNewViewController: UIViewController {

    var form: Form?

    .....

}

更新:

这里是存储库:https://github.com/FlorianLdt/LFEasyDelegate

有什么问题就问我

希望有帮助.

这篇关于将数据从 tableviewcontroller 快速传递到另一个 tableviewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 23:30