Marcelo Pontes Machado
Marcelo Pontes Machado

Reputation: 183

PrepareforSegue been called before IBAction

I'm trying to pass a value inside a variable to take the indexpath of UIButton inside a custom cell when I'm clik on it and pass this valor to another View Controller using a prepareForSegue:

var row2 = Int() 

@IBAction func Teste(sender: AnyObject) {
    var btnPos: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(btnPos)!
    var row2 = Int(indexPath.row)
    self.performSegueWithIdentifier("commentsIdentifier", sender: self)     
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    if (segue.identifier == "detailIdentifier") {
        let detailScene = segue.destinationViewController as DetailViewController
        // Pass the selected object to the destination view controller.
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let row = Int(indexPath.row)
            detailScene.currentObject = objects[row] as? PFObject

        }


    } else if (segue.identifier == "commentsIdentifier") {
        let commentScene = segue.destinationViewController as TesteButtonViewController
        println(row2)

    }

But, when I print the row2 inside the prepareforsegue I cannot see the value, I think it`s because the prepareforSegue is called before the IBAction, any ideas of how can I solve this?

Upvotes: 0

Views: 424

Answers (1)

luk2302
luk2302

Reputation: 57114

Your IBAction is called before prepareForSegue, your mistake is something else.
Just change the line:

var row2 = Int(indexPath.row)

to

row2 = Int(indexPath.row)

What is currently happening is that you create a local variable with function scope inside Teste that hides the variable of the same name of your class.

Upvotes: 2

Related Questions