我知道这是一个经常提问的问题,但我做了一些研究,并没有任何解决方案工作。将tableViewCell中的数据传递给另一个VC失败
所以这里是我的控制器表视图
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
}
}
有一个UIButton在我的tableView细胞
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的标题设置为“站点名称”。
所以在VC中用tableView,在cellforRowAt
函数中我调用了addTarget。 与moveToMapPage
功能
,但是当我拍了拍按钮并进入MapView的VC,在stationName
仍然是零
我不知道哪里出了问题, 任何提示的赞赏
嘿罗素谢谢回答!似乎我误解了导航的东西 – Ian