将tableviewcontroller中的数据传递给swift中的另一个tableviewcontroller

时间:2018-01-08 02:12:01

标签: swift tableview segue textfield

我有一个我正在制作的表格

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

我在使这个tableviewcontroller将数据传递给新的tableviewcontroller时遇到问题。我被困住了,不知道怎么回事。

enter image description here

enter image description here

enter image description here

enter image description here

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)

}

}

2 个答案:

答案 0 :(得分:1)

如何开始创建模型:

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时创建此表单实例:

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

class myNewViewController: UIViewController {

    var form: Form?

    .....

}

<强>更新

以下是回购:https://github.com/FlorianLdt/LFEasyDelegate

如果您有问题,请问我

希望它有所帮助。

答案 1 :(得分:0)

第一个选项 - 结构 - 首选

利用Structs:

struct Manager
{
 static var value : String = ""
}

Noe只需调用

即可更新该函数的值
Manager.value = "newValue"

随时随地访问该值将其分配给其他变量

let newStr : String = Manager.value

第二个选项 - AppDelegate - 不理想

在AppDelegate中创建新对象

enter image description here

现在创建一个新对象来访问appDelegate

let appDel = UIApplication.shared.delegate as! AppDelegate

访问价值并更新如下

appDel.Frequency = 1.0

第三个选项 - NSObjectClass

创建一个新的NSObject类,如下所示

enter image description here

// NSObject类首次加载到内存堆栈时创建的实例

static let shared = wrapperClass()

//在Object类

中创建一个全局访问的值
var newValueInClass : String = ""

现在是时候访问创建的对象

wrapperClass.shared.newValueInClass = "iosGeek"

Now Anywhere编写此行

print(wrapperClass.shared.newValueInClass)

控制台输出

enter image description here

最好使用结构化类来全局管理数据