如何快速将一个视图控制器值发送到另一个视图控制器表视图?
Posted
技术标签:
【中文标题】如何快速将一个视图控制器值发送到另一个视图控制器表视图?【英文标题】:How to send one view controller values to another view controller table view in swift? 【发布时间】:2020-06-01 09:28:59 【问题描述】:我有如下三个视图控制器
Add address Table view 这是 AddAddressViewcontroller,如果我选择 addaddress 按钮,它会转到 profileviewcontroller 如果我点击继续,它会转到 ZoomMapviewcontroller 这是我确认然后我需要在AddAddressViewcontroller
中显示下面的标签文本 添加地址表视图怎么做。
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)
这里如何将 zipName、locality、subLocality 值发送到 Addaddressviewcontroller tableview
AddressViewcontroller代码:
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
请帮我写代码,我卡在这里很久了
【问题讨论】:
【参考方案1】:这里有两种方法..
第一种方法是假设您在 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)
【讨论】:
谢谢,我如何在cell.addressLabel.text
中显示enteredData
请帮助
已编辑。您可以直接将 zipName、sublocalityName 和 localityName 传递给新的 AddAddressViewController。
在AddAddressViewController
如何显示numberOfRowsInSection
返回??
例如,如果我返回 2.. 它显示两次相同的值......如果我输入新地址,旧地址不会出现
请帮忙添加numberOfRowsInSection
以上是关于如何快速将一个视图控制器值发送到另一个视图控制器表视图?的主要内容,如果未能解决你的问题,请参考以下文章