如何从 swift 中的父视图控制器访问容器视图中的数据?
How can I access data from a container view from the parent view controller in swift?
我有一个视图控制器(SignUpController
),它在顶部有 1 个图像视图,在底部有 2 个按钮和 1 个视图 container view
。容器视图 (InfoRegisterController
) 包含带有多个文本字段的 scrollview
。容器视图不包含任何按钮。所以,现在我需要访问父视图控制器中容器视图的文本字段(或者可以说:我需要访问从容器视图到父视图控制器的所有数据),以便我可以通过单击注册按钮注册用户在 SignUpController
中垫底。我也无法通过代表和 NSUserDefaults
解决它。所以,请帮我解决这个问题 swift 4 ?
这是父视图控制器:
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)
}
所有数据都必须访问这个地方,这样我才能post它到服务器。
@IBAction func Register(_ sender: Any) {
}
} // class end
容器视图:
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
}
}
在这里,我需要访问 SignUpController 的所有字段 fullName、Email、Password、verifyPassword、address、permanentAddress、currentAddress、DOB、occupation 和所有其他字段,以便我可以注册新用户。
有几种方法可以实现这一点。您可以在父视图控制器中使用 "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
}
}
要从父级访问容器视图,您必须将引用从容器视图传递给父级,然后在父级视图控制器中使用它,有很多方法可以做到这一点,这里是其中之一。
在容器视图控制器 InfoRegisterController
的 viewDidAppear
中添加以下代码,此方法将获取 InfoRegisterController
的引用到要使用的父对象中。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let signUpControllerParent = self.parent as! SignUpController
signUpControllerParent.saveContrainerViewRefference(vc: self)
}
现在在 SignUpController
中添加一个本地变量,用于保存即将到来的引用并稍后用于从文本字段获取数据。
var infoRegisterRefferenceVC : InfoRegisterController?
在您的父级中也添加此方法SignUpController
func saveContrainerViewRefference(vc:InfoRegisterController){
self.infoRegisterRefferenceVC = vc
}
现在您可以从父级访问容器视图中的所有文本字段和方法,例如:
var fullNameTextField = self.infoRegisterRefferenceVC.fullName.text
应该是这样:)
我有一个视图控制器(SignUpController
),它在顶部有 1 个图像视图,在底部有 2 个按钮和 1 个视图 container view
。容器视图 (InfoRegisterController
) 包含带有多个文本字段的 scrollview
。容器视图不包含任何按钮。所以,现在我需要访问父视图控制器中容器视图的文本字段(或者可以说:我需要访问从容器视图到父视图控制器的所有数据),以便我可以通过单击注册按钮注册用户在 SignUpController
中垫底。我也无法通过代表和 NSUserDefaults
解决它。所以,请帮我解决这个问题 swift 4 ?
这是父视图控制器:
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)
}
所有数据都必须访问这个地方,这样我才能post它到服务器。
@IBAction func Register(_ sender: Any) {
}
} // class end
容器视图:
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
}
}
在这里,我需要访问 SignUpController 的所有字段 fullName、Email、Password、verifyPassword、address、permanentAddress、currentAddress、DOB、occupation 和所有其他字段,以便我可以注册新用户。
有几种方法可以实现这一点。您可以在父视图控制器中使用 "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
}
}
要从父级访问容器视图,您必须将引用从容器视图传递给父级,然后在父级视图控制器中使用它,有很多方法可以做到这一点,这里是其中之一。
在容器视图控制器 InfoRegisterController
的 viewDidAppear
中添加以下代码,此方法将获取 InfoRegisterController
的引用到要使用的父对象中。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let signUpControllerParent = self.parent as! SignUpController
signUpControllerParent.saveContrainerViewRefference(vc: self)
}
现在在 SignUpController
中添加一个本地变量,用于保存即将到来的引用并稍后用于从文本字段获取数据。
var infoRegisterRefferenceVC : InfoRegisterController?
在您的父级中也添加此方法SignUpController
func saveContrainerViewRefference(vc:InfoRegisterController){
self.infoRegisterRefferenceVC = vc
}
现在您可以从父级访问容器视图中的所有文本字段和方法,例如:
var fullNameTextField = self.infoRegisterRefferenceVC.fullName.text
应该是这样:)