如何将一个视图控制器值发送到 swift 中的另一个视图控制器 table 视图?
How to send one view controller values to another view controller table view in swift?
我有如下三个视图控制器
Add address Table view this is AddAddressViewcontroller, if i select addaddress button it goes to profileviewcontroller if i tap continue it goes to ZoomMapviewcontroller 这里是我确认然后我需要在 AddAddressViewcontroller
添加地址 Table 查看 中显示下面的标签文本怎么办那。
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、locality、subLocality 值 w 发送到 Addaddressviewcontroller tableview
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
}
}
请帮我写代码,我卡在这里很久了
这里有两种方法..
第一种方法假设您已在 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)
}
我有如下三个视图控制器
Add address Table view this is AddAddressViewcontroller, if i select addaddress button it goes to profileviewcontroller if i tap continue it goes to ZoomMapviewcontroller 这里是我确认然后我需要在 AddAddressViewcontroller
添加地址 Table 查看 中显示下面的标签文本怎么办那。
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、locality、subLocality 值 w 发送到 Addaddressviewcontroller tableview
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
}
}
请帮我写代码,我卡在这里很久了
这里有两种方法..
第一种方法假设您已在 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)
}