如何使用笔尖对不同的视图控制器执行segue?

时间:2015-10-11 05:01:15

标签: ios swift

目前我已成功将表视图nib放入视图控制器中,但是当单击nib中的单元格时,如何对视图控制器执行segue。我在视图控制器中有nib,视图控制器可以执行segue,但是当单击单元格时,nib文件将执行segue,它不能执行。我如何让segue工作?下面是笔尖的代码。

 override func viewDidLoad() {
    super.viewDidLoad()
    self.loadViewFromNib()
    let nib = UINib(nibName: "LiveConcerts", bundle: nil)
    table1.registerNib(nib, forCellReuseIdentifier: "cell")
}
func loadViewFromNib(){
    NSBundle.mainBundle().loadNibNamed("View1", owner: self, options: nil)
    print("in viewcontroller")
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfConcerts.count

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.table1.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell

    let concerts = arrayOfConcerts[indexPath.row]

    cell.setCell(concerts.imageName)

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}

1 个答案:

答案 0 :(得分:0)

'对于Swift 3,你可以这样做。您可以正常创建segue,从表格视图单元格到故事板中的另一个视图控制器。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // When selected perform our segue we set in the storyboard
    // Don't forget to set it's Storyboard Segue Identifier
    let cell = tableView.cellForRow(at: indexPath) as? TableViewCell
    self.performSegue(withIdentifier: "youSegueIdInTheStoryBoard", sender: cell)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Here we can check the segue performed above in the
    // didSelectRowAt indexPath function and 
    // Transfer any data to the destination view controller
    if (sender != nil && segue.identifier == "youSegueIdInTheStoryBoard") {
        let cell = sender as? TableViewCell
        let indexPath:IndexPath = self.tableView.indexPath(for: cell!)!
        // segue.destination is where you're now going, you can
        // cast it to whatever type it is and set things on it
    }
}