JMicro JMicro - 9 days ago 6
Swift Question

Segue is not performed iOS8

I am using XCode 6, targeting iOS 8 and coding this in Swift.

My storyboard controllers look like this:

Tab Bar > Navigation > Table View > Detail View


The
Show Detail
segue is from a
Table Cell
to the
Detail View
.

The
prepareForSegue
method is not triggered when clicking a table cell. Segue from a button to detail works fine, though.
performSegueWithIdentifier
in
didSelectRowAtIndexPath
also works fine.

I have also created a brand new test project to test this issue - the controller code looks like this:

import UIKit

class TestTableController: UITableViewController
{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 2;
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellId = "testCell";
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell;

if (cell == nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellId);
}

cell!.textLabel!.text = "cell \(indexPath.item)";

return cell!;
}
}


Any idea why this does not work out of the box?

P.S.: when using
Split View
instead of a
Tab Bar
, the same segue works fine.

Answer

I just tested it with a sample project. It works fine. There must be something wrong with your wiring up the cell.

Here is my code. Only indicating changes from plain vanilla tab bar project template.

enter image description here

// FirstViewController.swift
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
    cell.textLabel?.text = "Cell \(indexPath.row + 1)"
    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let detail = segue.destinationViewController as DetailController
    let cell = sender as UITableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)
    detail.detailItem = "Cell \(indexPath!.row + 1)"
}

//DetailController.swift
class DetailController: UIViewController {

    var detailItem : String!
    @IBOutlet var label : UILabel!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        label.text = detailItem
    }
}
Comments