将数据从tableviewcontroller传递到另一个tableviewcontroller。

时间:2023-01-16 16:43:02

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.

此表单将填充用户输入的textfields。回答完所有问题后,会弹出一个按钮保存。

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.

我有一个问题让这个tableviewcontroller传递数据给一个新的tableviewcontroller。我被困住了,不知道该怎么做。

将数据从tableviewcontroller传递到另一个tableviewcontroller。

将数据从tableviewcontroller传递到另一个tableviewcontroller。

将数据从tableviewcontroller传递到另一个tableviewcontroller。

将数据从tableviewcontroller传递到另一个tableviewcontroller。

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 个解决方案

#1


1  

what about starting creating a model:

如何开始创建一个模型:

Form.swift

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:

现在您可以在保存数据并将数据推送到新的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?

    .....

}

UPDATE:

更新:

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

这里是repo: https://github.com/FlorianLdt/LFEasyDelegate

If you have some question just ask me

如果你有什么问题就问我

Hope it helps.

希望它可以帮助。

#2


0  

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

第二个选项- AppDelegate -不理想

Create new object in AppDelegate

在AppDelegate中创建新对象

将数据从tableviewcontroller传递到另一个tableviewcontroller。

Now create a new object to access appDelegate

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

let appDel = UIApplication.shared.delegate as! AppDelegate

Access Value and update as below

访问值和更新如下。

appDel.Frequency = 1.0

Third Option - NSObjectClass

第三种选择——NSObjectClass

Create a new NSObject class as below

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

将数据从tableviewcontroller传递到另一个tableviewcontroller。

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

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

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

控制台输出

将数据从tableviewcontroller传递到另一个tableviewcontroller。

Better to use struct classes to manage data globally

更好地使用struct类来管理全局数据。

#1


1  

what about starting creating a model:

如何开始创建一个模型:

Form.swift

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:

现在您可以在保存数据并将数据推送到新的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?

    .....

}

UPDATE:

更新:

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

这里是repo: https://github.com/FlorianLdt/LFEasyDelegate

If you have some question just ask me

如果你有什么问题就问我

Hope it helps.

希望它可以帮助。

#2


0  

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

第二个选项- AppDelegate -不理想

Create new object in AppDelegate

在AppDelegate中创建新对象

将数据从tableviewcontroller传递到另一个tableviewcontroller。

Now create a new object to access appDelegate

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

let appDel = UIApplication.shared.delegate as! AppDelegate

Access Value and update as below

访问值和更新如下。

appDel.Frequency = 1.0

Third Option - NSObjectClass

第三种选择——NSObjectClass

Create a new NSObject class as below

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

将数据从tableviewcontroller传递到另一个tableviewcontroller。

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

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

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

控制台输出

将数据从tableviewcontroller传递到另一个tableviewcontroller。

Better to use struct classes to manage data globally

更好地使用struct类来管理全局数据。