7

i have a custom cell attached to TableViewCell class, and i have a button inside that custom cell, i want whey i press the button it segues to another view controller, but the:

performSegueWithIdentifier(identifier: String, sender: AnyObject?)

function is not recognised, how to fix that ?

Edit:

Screenshot

Abdou023
  • 1,654
  • 2
  • 24
  • 45

3 Answers3

19

-performSegueWithIdentifier: method is declared in UIViewController. So you can't just call it in UITableViewCell subclass.

You can add an action to that button when you are creating cell in -tableView:cellForRowAtIndexpath: method. Then you can call -performSegueWithIdentifier: method in that action method. Here is example assuming we are in UITableViewController subclass:

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

    cell.button.addTarget(self, action: "someAction", forControlEvents: .TouchUpInside)

    return cell
}

And here is action method:

func someAction() {
    self.performSegueWithIdentifier("moveToView", sender: self)
}
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
mustafa
  • 15,254
  • 10
  • 48
  • 57
  • This solves a problem I spent hours on. Thanks. Is this a best practice? It's easy and it works, so that's a good thing. Consideration to take into account is preparing segue to pass data to the moveToView – Ryan Walton Mar 07 '15 at 22:48
  • please show how you define your segue... Because it is not working for me ;( – Arti Oct 11 '15 at 12:47
1

You need a custom class for your cell. In that class, create an @IBAction as response to the button click. In that action, perform your segue.

zisoft
  • 22,770
  • 10
  • 62
  • 73
  • 1
    `performSegueWithIdentifier` is a function of UIViewController. You can create an `@IBOutlet` in your custom cell class and connect it to the TableViewController it belongs to. Then you are able to perform the segue with `myTableViewController.performSegueWithIdentifier` – zisoft Oct 01 '14 at 15:07
0

If you are using Interface Builder:

Connect your firstViewController to the secondViewController - and create a Segue Identifier.

Then you are able to use

performSegueWithIdentifier(identifier: String, sender: AnyObject?)
derdida
  • 14,784
  • 16
  • 90
  • 139