我有如下三个视图控制器
Add address Table view这是AddAddressViewcontroller,如果我选择addaddress按钮,它将转到profileviewcontroller,如果我点击继续,它将转到ZoomMapviewcontroller,这是我确认的信息,那么我需要在标签文本下方显示在AddAddressViewcontroller
中添加地址表视图。
ZoomMapviewcontroller代码:
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)
}
}
此处介绍如何将zipName,位置,subLocality值w发送到Addaddressviewcontroller表视图
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
}
}
请帮助我编写代码,很长时间以来我一直被困在这里
答案 0 :(得分:0)
这里有2种方法。
第一种方法是假设您已在AddAddressViewController中正确设置了DataEnteredDelegate。您的错误是,您正在NavigationController堆栈中已经存在的AddAddressViewcontroller实例中调用委托方法,然后将新的AddAddressViewcontroller再次推入该堆栈中。
第二种方法,如果您确实要(按照我的观点,这是不必要的)将新的AddAddressViewController实例再次推送到navigationController堆栈。
第一种方法:- 您在navigationController堆栈中已经有一个AddAddressViewController实例。因此,您只需要弹出回到该视图控制器。因此,您在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
}
}
}
}
第二种方法:-在这种情况下,无需委派。您可以直接将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)
}