gpt4 book ai didi

ios - 将 tableViewCell 中的数据传递给另一个 VC 失败

转载 作者:行者123 更新时间:2023-11-28 10:14:59 24 4
gpt4 key购买 nike

我知道这是一个经常被问到的问题,但我做了一些研究,但没有一个解决方案有效。

所以这是我的带有表格 View 的 Controller

class MainPage: UIViewController, UITableViewDelegate, UITableViewDataSource, YoubikeManagerDelegate {
@IBOutlet weak var tableView: UITableView!

let mainPageCell = MainPageCell()
let mapPage = MapViewController()

var stations: [Station] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "infoCell") as! MainPageCell
cell.stationNameLabel.text = stations[indexPath.row].name
cell.stationLocationLabel.text = stations[indexPath.row].address
cell.numberOfRemainingBikeLabel.text = stations[indexPath.row].numberOfRemainingBikes
cell.printer = stations[indexPath.row].name
cell.mapBtn.tag = indexPath.row
cell.mapBtn.addTarget(self, action: #selector(moveToMapPage), for: .touchUpInside)
return cell
}
func moveToMapPage(sender: UIButton) {
self.performSegue(withIdentifier: "toMap", sender: self)
let nameToPass = stations[sender.tag].name
mapPage.stationName = nameToPass
}

我的 tableView 单元格中有一个 UIButton

class MainPageCell: UITableViewCell {
var printer: String!
let mapPage = MapViewController()
@IBOutlet weak var stationNameLabel: UILabel!
@IBOutlet weak var mapBtn: UIButton!
@IBOutlet weak var stationLocationLabel: UILabel!
@IBOutlet weak var numberOfRemainingBikeLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
mapBtn.addTarget(self, action: #selector(mapBtnTapped), for: .touchUpInside)

}
func mapBtnTapped (sender: UIButton) {
print (printer)

}
}

这是我的另一个VC

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!

var stationName: String = ""

override func viewDidLoad() {
super.viewDidLoad()
self.title = stationName
}

我将详细说明我现在面临的问题!我想做的是当我点击 tableView 单元格中的按钮时,我想转到 MapViewController 并在同一个单元格中将此 vc 的标题设置为“站名”。

所以在带有 tableView 的 VC 中,在 cellforRowAt 函数中我调用了 addTarget。使用 moveToMapPage 函数

但是当我点击按钮并转到 MapView VC 时,stationName 仍然是 nil

我不知道哪里出了问题,任何提示表示赞赏

最佳答案

mapPage 不是您要导航到的实例,因此您要在不相关的 Controller 上设置变量。

如果你想获得新 Controller 的链接,你需要使用prepare(for segue...

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
// if you have multiple segues, you can set up different actions for each
if segue.identifier == "segueToMap"
{
let mapPage : MapViewController = segue.destination as! MapViewController

let mapPage : MapViewController = segue.destination as! MapViewController
mapPage.stationName = nameToPass
mapPage.delegate = self // if required
}
}

关于ios - 将 tableViewCell 中的数据传递给另一个 VC 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42455366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com