0

I have prototype cell and in this cell there is an UIButton; so I need to perform a segue from the button to other controller, not from the cell. I tried to create an Outlet @IBOutlet var button: UIButton! and use performSegueWithIdentifier() method

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    self.performSegueWithIdentifier("toSecondController", sender: button)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

but I have no idea how to set the prepareForSegue() method... i tried this

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
    if segue.identifier == "toSecondController"
    {
        if let indexPath = sender as? UIButton//tableView.indexPathForSelectedRow()
        {
            let controller = segue.destinationViewController as! CoriViewController
            controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row]
        }
    }
}

I also read that a solution would be implement a protocol, but I'm a beginner and I need a step by step guide. Hope someone can help me!

Fabio Cenni
  • 841
  • 3
  • 16
  • 30

1 Answers1

0

Your on the right track. In your tableviewcontroller define

@IBAction func clicked(){
    self.performSegueWithIdentifier("toSecondController", sender: button)
}

You can keep the

    tableView.deselectRowAtIndexPath(indexPath, animated: true)

line in your didSelectRowAtIndexPath. Don't forget to connect your ibaction to the button in your storyboard as well.

Roosh
  • 97
  • 1
  • 8
  • 's thanks a lot! But I have also a problem with the segue "uibutton does not have a member named row (line 8)! – Fabio Cenni Aug 31 '15 at 22:47
  • change sender:button to sender:self – Roosh Aug 31 '15 at 23:07
  • Sorry but I'm not able to make the prepareForSegue working... could you help me with this other method? Xcode still shows previous error. – Fabio Cenni Aug 31 '15 at 23:19
  • Change toCoriViewController to toSecondController and it should be all cleared up – Roosh Sep 01 '15 at 01:55
  • Sorry it was a coping and pasting mistake... this is the line that give me error controller.coriSquadra = DataManager.sharedInstance.arrayCori[indexPath.row] – Fabio Cenni Sep 01 '15 at 02:01
  • where do you declare arrayCori? You could try self.arrayCori[indexPath.row] – Roosh Sep 02 '15 at 22:00
  • Finally I solved with this other question http://stackoverflow.com/questions/32341629/prepareforsegue-from-a-uibutton-in-a-custom-prototype-cell – Fabio Cenni Sep 04 '15 at 22:51