在 IBAction 之前调用 PrepareforSegue
PrepareforSegue been called before IBAction
我正在尝试在变量中传递一个值,以便在我点击它时在自定义单元格中获取 UIButton 的索引路径,并使用 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)
}
但是,当我在 prepareforsegue 中打印 row2 时,我看不到该值,我认为这是因为在 IBAction 之前调用了 prepareforSegue,请问我该如何解决这个问题?
你的IBAction
在prepareForSegue
之前被调用了,你的错误是别的。
只需更改行:
var row2 = Int(indexPath.row)
到
row2 = Int(indexPath.row)
目前正在发生的事情是,您在 Teste
中创建了一个具有函数作用域的局部变量,它隐藏了与您的 class.
同名的变量
我正在尝试在变量中传递一个值,以便在我点击它时在自定义单元格中获取 UIButton 的索引路径,并使用 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)
}
但是,当我在 prepareforsegue 中打印 row2 时,我看不到该值,我认为这是因为在 IBAction 之前调用了 prepareforSegue,请问我该如何解决这个问题?
你的IBAction
在prepareForSegue
之前被调用了,你的错误是别的。
只需更改行:
var row2 = Int(indexPath.row)
到
row2 = Int(indexPath.row)
目前正在发生的事情是,您在 Teste
中创建了一个具有函数作用域的局部变量,它隐藏了与您的 class.