Access data from container view instantly to parent view controller in swift 4

拜拜、爱过 提交于 2020-01-06 06:36:48

问题


I have a view controller(SignUpController) and it has 1 image view at the top, 2 buttons at the bottom and 1 view as container view. The Container view(InfoRegisterController) contains scrollview with several text fields. Container view doesn’t contain any buttons. So, now i need to access text fields of Container view in the parent view controller (or can say as : I need to access all data from container view to parent view controller) so that I can register the user by clicking Register button at the bottom in SignUpController. I couldn’t solve it through delegates and NSUserDefaults too. So , Please help me to solve this in swift 4 ?

This is the parent view controller:

import UIKit
import Material

class SignUpController: UIViewController {  


@IBOutlet weak var backgroundView: UIView!

@IBOutlet weak var register: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    backgroundView.layer.borderWidth = 1
    backgroundView.layer.borderColor = UIColor.blue.cgColor
    register.layer.cornerRadius = 5 

    // Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated) // No need for semicolon

    let tv: InfoRegisterController = self.childViewControllers[0] as!InfoRegisterController

    print("Hello")
     //tv.viewWillAppear(true) 
 }

All data must be accessed to this place so that I can post it to server.

@IBAction func Register(_ sender: Any) {




    }
} // class end

Container View:

import UIKit
import Material

class InfoRegisterController: UIViewController, UITextFieldDelegate,  UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

@IBOutlet weak var fullName: TextField!
@IBOutlet weak var Email: ErrorTextField!
@IBOutlet weak var Password: TextField!
@IBOutlet weak var verifyPassword: TextField!
@IBOutlet weak var address: TextField!
@IBOutlet weak var permanentAddress: TextField!
@IBOutlet weak var currentAddress: TextField!
@IBOutlet weak var DOB: TextField!
@IBOutlet weak var occupation: TextField!
@IBOutlet weak var selectGender: TextField!
@IBOutlet weak var identificationType: TextField!
@IBOutlet weak var identificationID: TextField!
@IBOutlet weak var issuedDate: TextField!
@IBOutlet weak var issuedDistrict: TextField!
@IBOutlet weak var licenseNumber: TextField!
@IBOutlet weak var fathersName: TextField!
@IBOutlet weak var grandfathersName: TextField!
@IBOutlet weak var mobileNumber: TextField!

override func viewDidLoad() {
    super.viewDidLoad()

    self.fullName.delegate = self
    self.Email.delegate = self
    self.Password.delegate = self
    self.verifyPassword.delegate = self
    self.address.delegate = self
    self.permanentAddress.delegate = self
    self.currentAddress.delegate = self
    self.DOB.delegate = self
    self.occupation.delegate = self
    self.identificationID.delegate = self
    self.issuedDate.delegate = self
    self.issuedDistrict.delegate = self
    self.licenseNumber.delegate = self
    self.fathersName.delegate = self
    self.grandfathersName.delegate = self
    self.mobileNumber.delegate = self

    }  
}

Here, I need to access all fields fullName,Email, Password,verifyPassword,address, permanentAddress,currentAddress, DOB,occupation and all others to SignUpController so that I can register new user.


回答1:


To gain access to the container view from your parent, you will have to pass a reference from the container view to the parent and then use it in the parent view controller, there are many ways to do that and here is one of them.

In your viewDidAppear of InfoRegisterController which is the container view controller add the following code, this method will get a reference of InfoRegisterController into the parent to be used.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let signUpControllerParent = self.parent as! SignUpController
    signUpControllerParent.saveContrainerViewRefference(vc: self)

}

Now in SignUpController add a local variable for the coming reference to be saved and used later to get the data from the textfields.

var infoRegisterRefferenceVC : InfoRegisterController?

Add this method also in your parent SignUpController

func saveContrainerViewRefference(vc:InfoRegisterController){

    self.infoRegisterRefferenceVC = vc

}

Now you can have access to all the textfields and the methods in the container view from the parent for example:

var fullNameTextField = self.infoRegisterRefferenceVC.fullName.text 

This should be it :)




回答2:


There are several ways to accomplish this. You can use "Delegate" and "Protocol" or prepareForSegue method in parent view controller.

var somePropertyYouWantToAccess: NSString?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

  if segue.identifier == "yourChildViewSegueIdentifier" {
       let childVc = segue.destinationViewController as ContainerViewController
        self.somePropertyYouWantToAccess = childVc.firstName
    }
}


来源:https://stackoverflow.com/questions/47411431/access-data-from-container-view-instantly-to-parent-view-controller-in-swift-4

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