[英]How to send one view controller values to another view controller table view in swift?
I have three view controllers like below我有三个视图控制器,如下所示
Add address Table view this is AddAddressViewcontroller, if i select addaddress button it goes to profileviewcontroller if i tap continue it goes to ZoomMapviewcontroller here is i confirm then i need to show that below label text in AddAddressViewcontroller
Add address Table view how to do that.添加地址表视图这是 AddAddressViewcontroller,如果我 select addaddress 按钮转到profileviewcontroller如果我点击继续它转到ZoomMapviewcontroller这是我确认然后我需要在AddAddressViewcontroller
添加地址表视图下面显示 label 文本如何做到这一点。
ZoomMapviewcontroller code: ZoomMapview控制器代码:
protocol DataEnteredDelegate: class {
func userDidEnterInformation(info: DataEnteredModelSave)
}
class NewZoomAddressViewController: UIViewController {
weak var delegate: DataEnteredDelegate? = nil
var zipName: String?
var localityName: String?
var sublocalityName: String?
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func confirmBtn(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
guard
let zipName = zipName,
let sublocalityName = sublocalityName,
let localityName = localityName
else { return }
let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
delegate?.userDidEnterInformation(info: enteredData)
self.navigationController?.pushViewController(viewController, animated: true)
}
}
here how to send zipName, locality, subLocality valuew to Addaddressviewcontroller tableview这里如何将 zipName、locality、subLocality valuew 发送到 Addaddressviewcontroller tableview
AddAddressViewcontroller code: AddAddressViewcontroller代码:
class AddAddressViewController: UIViewController,DataEnteredDelegate {
@IBOutlet var addressEDITTableview: UITableView!
var city: String?
var pincode: String?
var locality: String?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
cell.nameHeader.text = "header"
cell.addressLabel.text = "\(city!) \(pincode!) \(locality!)" // here i need ZoomMapviewcontroller valuew in row
}
}
please help me with code, i got stuck here from long time请帮我写代码,我卡在这里很久了
There are 2 approaches here..这里有两种方法..
First method is assuming you have correctly set the DataEnteredDelegate in the AddAddressViewController.第一种方法是假设您在 AddAddressViewController 中正确设置了 DataEnteredDelegate。 Your mistake here is, you are calling a delegate method in an already existing AddAddressViewcontroller instance in the navigationController stack and pushing a new AddAddressViewcontroller into that stack again.您的错误是,您在 navigationController 堆栈中已经存在的 AddAddressViewcontroller 实例中调用委托方法,并再次将新的 AddAddressViewcontroller 推入该堆栈。
Second method, if you really want to push (which is unnecessary as per me) a new AddAddressViewController instance to the navigationController stack again.第二种方法,如果你真的想再次将一个新的 AddAddressViewController 实例推送到 navigationController 堆栈(这对我来说是不必要的)。
First approach :- You've already an AddAddressViewController instance in your navigationController stack.第一种方法:-您的 navigationController 堆栈中已经有一个 AddAddressViewController 实例。 So you just need to pop back to that view controller.所以你只需要回到那个视图 controller。 So your confirmBtn action in NewZoomAddressViewController will be:-因此,您在 NewZoomAddressViewController 中的 confirmBtn 操作将是:-
@IBAction func confirmBtn(_ sender: Any) {
guard
let zipName = zipName,
let sublocalityName = sublocalityName,
let localityName = localityName
else { return }
if let viewControllers = self.navigationController?.viewControllers {
for aViewController in viewControllers {
if aViewController.isKind(of: AddAddressViewController.self) {
let addAddressVC = aViewController as? AddAddressViewController
let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
delegate?.userDidEnterInformation(info: enteredData)
self.navigationController?.popToViewController(aViewController, animated: true)
break
}
}
}
}
Second Approach :- In this case there is no need for delegate.第二种方法:- 在这种情况下,不需要委托。 You can directly pass the zipName, sublocalityName and localityName to the new AddAddressViewController.您可以直接将 zipName、sublocalityName 和 localityName 传递给新的 AddAddressViewController。
@IBAction func confirmBtn(_ sender: Any) {
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
guard
let zipName = zipName,
let sublocalityName = sublocalityName,
let localityName = localityName
else { return }
viewController.city = localityName
viewController.pincode = zipName
viewController.locality = localityName
self.navigationController?.pushViewController(viewController, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.