【发布时间】:2015-05-26 17:00:23
【问题描述】:
我在 uicollectioncell 中有两个按钮,我想选择一个,然后根据第一个按钮的状态对另一个执行操作。 我试过这个,但它抛出:“快速致命错误:在展开可选值时意外发现 nil”
func firstAction(sender:UIButton) {
var cell = Customcell()
cell.firstButton.selected = true
cell.firstButton.selected = !cell.firstButton.selected
}
@IBAction func secondAction(sender: UIBarButtonItem) {
var cell = CustomCell()
if cell.firstButton.selected == true {
// DO stuff....
} else {
println("No button selected")
}
}
我在 cellForItemAtIndexPath 中这样设置按钮:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomCell
cell.firstButton.addTarget(self, action: "firstAction:", forControlEvents: UIControlEvents.TouchUpInside)
cell.secondButton.addTarget(self, action: "secondAction:", forControlEvents: UIControlEvents.TouchUpInside)
我该如何解决这个问题?
【问题讨论】:
-
错误出现在哪一行?
-
这里:cell.firstButton.selected = true
-
我猜
firstButton是IBOutlet?如果您手动实例化您的单元格,则不会加载笔尖。这意味着firstButton不会被连接,它将为零。 -
是的,我的 firstButton 是一个 IBOutlet。如何不手动实例化单元格?
-
感谢您对此感兴趣!
标签: ios swift button uicollectionview cell