pass data from tableviewcontroller to another tableviewcontroller in swift

南楼画角 提交于 2019-12-08 02:46:34

问题


I have a form I am creating

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

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)

}

}

回答1:


what about starting creating a model:

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
    }

}

now you can create this form instance when saving and pushing the data to the new 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?

    .....

}

UPDATE:

Here is the repo: https://github.com/FlorianLdt/LFEasyDelegate

If you have some question just ask me

Hope it helps.




回答2:


First Option - Structs - Preferred

Make use of Structs :

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

Noe Update value of that function by just calling

Manager.value = "newValue"

Access that value anywhere Assign it to other Variables

let newStr : String = Manager.value

Second Option - AppDelegate - Not ideal

Create new object in AppDelegate

Now create a new object to access appDelegate

let appDel = UIApplication.shared.delegate as! AppDelegate

Access Value and update as below

appDel.Frequency = 1.0

Third Option - NSObjectClass

Create a new NSObject class as below

//Instance created when NSObject class is first time loaded in memory stack

static let shared = wrapperClass()

//Create a value to access Globally in Object class

var newValueInClass : String = ""

Now time to access that created Object

wrapperClass.shared.newValueInClass = "iosGeek"

Now Anywhere write this Line

print(wrapperClass.shared.newValueInClass)

Console Output

Better to use struct classes to manage data globally



来源:https://stackoverflow.com/questions/48143437/pass-data-from-tableviewcontroller-to-another-tableviewcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!