有三个场景
adminSignup(Parent) --> It has two text field country and city
countrySelectorView(Child of adminSignup) --> It has picker view to select country
citySelectorView(Child of adminSignup) --> It has picker view to select city
当单击adminSignup的国家/地区文本字段时,将调用countrySelectorView场景,用户将选择该国家/地区,然后它将返回到adminSignup场景,国家/地区值将传递给adminSignup。
但是当点击城市文本字段时,将调用countrySelectorView场景,用户将选择该国家/地区,然后它将返回到adminSignup场景,并且国家/地区值将传递给adminSignup并且国家/地区文本字段的值将被删除。
以下是adminSignUp的代码
override func viewDidLoad() {
super.viewDidLoad()
if let passedCityValue = cityValue {
cityTextFeild.text = passedCityValue
}
if let currentCountryValue = countryValue {
countryTextFeild.text = currentCountryValue
}
}
以下是countrySelectorView的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let instanceOfAdminSignUp = segue.destination as? adminSignUp else { return }
instanceOfAdminSignUp.countryValue = countryValue
}
以下是citySelectorView的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let instanceOfAdminSignUp = segue.destination as? adminSignUp else { return }
instanceOfAdminSignUp.cityValue = cityValue
}
答案 0 :(得分:1)
请按照以下步骤
1- adminSignup查看控制器:
使用委托设计模式进行2个ViewControllers之间的通信
protocol AdminSignUPDelegate: class {
func cityValuePicked(value: String)
func countryValuePicked(value: String)
}
extension adminSignUp: AdminSignUPDelegate {
func cityValuePicked(value: String) {
cityTextFeild.text = value
}
func countryValuePicked(value: String) {
countryTextFeild.text = value
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? countrySelectorController {
destinationViewController.delegate = self
}
if let destinationViewController = segue.destination as? citySelectorController {
destinationViewController.delegate = self
}
}
2-城市选择ViewController
// Property
var delegate: AdminSignUPDelegate?
@IBAction func btnOKPressed(_ sender: Any) {
delegate?.cityValuePicked(value: cityValue)
dismiss(animated: true, completion: nil)
}
3-国家选择ViewController
// Property
var delegate: AdminSignUPDelegate?
@IBAction func btnOKPressed(_ sender: Any) {
delegate?.countryValuePicked(value: countryValue)
dismiss(animated: true, completion: nil)
}
注意:从国家/地区删除Segue&城市视图控制器到父级
如何从这里下载代码的工作示例
https://drive.google.com/file/d/1lA5ewSvw6KGyLW4XKQm10wSDE9dl_2PB/view?usp=sharing