2017-02-25 53 views
0

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

我不知道哪裏出了問題, 任何提示的讚賞

回答

3

mapPage您正在導航的實例,因此您要在不相關的控制器上設置變量。

您需要使用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 
    }  
} 
+0

嘿羅素謝謝回答!似乎我誤解了導航的東西 – Ian

1

使用導航控制器

let vc = self.storyboard?.instantiateViewController(withIdentifier:"toMap") as! toMapViewController 
vc.stationNam = stations[sender.tag].name 
self.navigationController?.pushViewController(vc, animated: true) 
+0

嘿Chetan,謝謝你的回答!你的代碼有助於很多! – Ian