Search code examples
uiviewcontrollerswiftuibuttoncustom-cellxcode6-beta5

Create Segue from Custom Cell UIButton to a ViewController


I want to create a segue from (a) a UIButton (in a custom cell of a tableview) and (b) a custom cell in general to a UIViewController. The custom cell is specified in a .xib file. I read that I just should control drag from the UIButton / custom cell to the UIViewController. That doesn't work for me.

  1. The .xib file of the custom cell isn't inside the storyboard - that's right? I thought about that it should be inside the storyboard. If necessary, how can I do that?
  2. How can I create a segue from the UIButton (in a custom cell) / custom cell to the UIViewController?

Thanks!

EDIT: I'm using a UITableViewController as Source, the destination is a UIViewController with a UITableView.


Solution

  • Ctrl-drag from the tableViewController (not from the prototype cell) to your target viewController and assign an identifier name to the segue.

    Now override didSelectRowAtIndexPathto perform the segue with your identifier when tapping that cell:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)    
    {
        performSegueWithIdentifier("yourSegue", sender: nil)
    }
    

    Distinguish between the cells based on your indexPath and create as many segues as you need to your target viewControllers.

    You can do similar for the button in your custom cell. Perform the segue on the button's action.