Ios 在UICollectionView中实现按钮单击

Ios 在UICollectionView中实现按钮单击,ios,ipad,delegates,uicollectionview,uicollectionviewcell,Ios,Ipad,Delegates,Uicollectionview,Uicollectionviewcell,是否有一种方法可以从UICollectionViewCell中的按钮获取按钮单击事件?我使用了一个nib来填充集合视图,单元格有按钮,但是它的操作没有被调用。我认为问题在于代表被叫来了。我怎样才能解决这个问题 我如何创建: 添加了一个空nib,创建了一个集合视图单元格 添加了一个.h和.m文件,并使cell nib的文件成为类创建时的所有者 在课堂上写了一个动作 将按钮连接到动作 有没有办法让我采取行动? 我做错了什么?添加如下按钮操作: - (UICollectionViewCell *)co

是否有一种方法可以从
UICollectionViewCell
中的按钮获取按钮单击事件?我使用了一个nib来填充集合视图,单元格有按钮,但是它的操作没有被调用。我认为问题在于代表被叫来了。我怎样才能解决这个问题

我如何创建:

  • 添加了一个空nib,创建了一个集合视图单元格
  • 添加了一个.h和.m文件,并使cell nib的文件成为类创建时的所有者
  • 在课堂上写了一个动作
  • 将按钮连接到动作
  • 有没有办法让我采取行动?
    我做错了什么?

    添加如下按钮操作:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:[indexPath row]]; 
    
        [[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    
    }
    
    
    - (IBAction)myClickEvent:(id)sender event:(id)event {
    
        NSSet *touches = [event allTouches];
    
        UITouch *touch = [touches anyObject];
    
        CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray];
    
        NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition];
    
    }
    

    通过从“对象”面板拖动“集合视图单元格”,在Nib中创建单元格非常重要。如果使用UIView并在Identity Inspector中更改此单元格的类,则该操作将不起作用。

    以下是swift 3.1代码

    // make a cell for each cell index path
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        // get a reference to our storyboard cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! BlueCircleViewCell
    
        // Use the outlet in our custom class to get a reference to the UILabel in the cell
        cell.bgImage.image = UIImage(named: items[indexPath.row])
        cell.addButton.addTarget(self, action: #selector(addCircle(_:)), for: .touchUpInside)
    
    //        cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
    
        return cell
    }
    
    func addCircle(_ sender:UIButton){
        //CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
        let buttonPosition:CGPoint = sender.convert(.zero, to: self.collectionView)
        let indexPath:IndexPath = self.collectionView.indexPathForItem(at: buttonPosition)!
        onAddBlueCircle(indexPath: indexPath)
    }
    

    你在FileOwner中有这个函数吗?请尝试删除指向操作的链接并重新连接。是否关闭请求?为什么?Oh ok ok EditedFile owner是nib的类本身吗?是的,是的。这也是为什么我可以正确连接插座的原因?这正是我的问题。很高兴我找到了这个建议。