[英]How can I access data from a container view from the parent view controller in swift?
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
.我有一个视图控制器(
SignUpController
),顶部有 1 个图像视图,底部有 2 个按钮,1 个视图作为container view
。 The Container view( InfoRegisterController
) contains scrollview
with several text fields.容器视图(
InfoRegisterController
)包含带有多个文本字段的scrollview
视图。 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
.所以,现在我需要访问父视图控制器中容器视图的文本字段(或者可以说:我需要访问从容器视图到父视图控制器的所有数据),以便我可以通过单击注册按钮来注册用户在
SignUpController
底部。 I couldn't solve it through delegates and NSUserDefaults
too.我也无法通过委托和
NSUserDefaults
解决它。 So , Please help me to solve this in swift 4 ?所以,请帮我在 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.在这里,我需要访问 SignUpController 的所有字段 fullName、Email、Password、verifyPassword、address、permanentAddress、currentAddress、DOB、occupation 和所有其他字段,以便我可以注册新用户。
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.在容器视图控制器
InfoRegisterController
的viewDidAppear
中添加以下代码,此方法会将InfoRegisterController
的引用获取到要使用的父级中。
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.现在在
SignUpController
为即将到来的引用添加一个局部变量,以便保存并稍后用于从文本字段中获取数据。
var infoRegisterRefferenceVC : InfoRegisterController?
Add this method also in your parent SignUpController
在您的父
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 :)应该是这样:)
There are several ways to accomplish this.有几种方法可以实现这一点。 You can use "Delegate" and "Protocol" or
prepareForSegue
method in parent view controller.您可以在父视图控制器中使用“Delegate”和“Protocol”或
prepareForSegue
方法。
var somePropertyYouWantToAccess: NSString?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yourChildViewSegueIdentifier" {
let childVc = segue.destinationViewController as ContainerViewController
self.somePropertyYouWantToAccess = childVc.firstName
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.