我知道這是一個經常提問的問題,但我做了一些研究,並沒有任何解決方案工作。將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