我正在尝试在变量中传递一个值,以便在我对其进行clik时将UIButton的索引路径置于自定义单元格中,并使用prepareForSegue将此valor传递给另一个View Controller:
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)
}
但是,当我在prepareforsegue中打印row2时,我看不到它的价值,我认为这是因为在IBAction之前调用了prepareforSegue,我有什么想法可以解决这个问题?
答案 0 :(得分:2)
在IBAction
之前调用prepareForSegue
,您的错误就是其他内容
只需更改一行:
var row2 = Int(indexPath.row)
到
row2 = Int(indexPath.row)
目前正在发生的是您创建一个局部变量,其函数范围在Teste
内,隐藏了与您同类名称相同的变量。