pass data from tableviewcontroller to another tableviewcontroller in swift

£可爱£侵袭症+ 提交于 2019-12-06 06:19:10

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.

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

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