为什么程序在单击按钮时崩溃?

时间:2015-11-02 18:03:36

标签: ios swift

我有一个带按钮的自定义单元格,我正在尝试添加一个动作。由于某种原因,应用程序崩溃时出现错误unrecognized selector sent to instance。我该如何解决?我的语法似乎是正确的。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("searchcell", forIndexPath: indexPath) as! searchcustomcell

 //follow is the name of the button in the custom cell
       cell.follow.addTarget(self, action: "FollowUser:", forControlEvents: UIControlEvents.TouchUpInside)


    func FollowUser(sender: UIButton){
        var relation: PFRelation = (PFUser.currentUser()?.relationForKey("Friendship"))!
        relation.addObject(users[indexPath.row])

 return cell
    }

1 个答案:

答案 0 :(得分:1)

  

由于某种原因,应用程序崩溃,错误无法识别的选择器被发送到实例。

不是“出于某种原因”。那的原因。您正在向实例发送无法识别的选择器。问题是你的FollowUser函数不是一种方法;它隐藏在collectionView:cellForItem...方法中。剪掉它,然后将它粘贴到班级的顶层:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // ...
}
func FollowUser(sender: UIButton){
    // ...
}
相关问题