Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法识别的选择器发送到UIButton中的实例_Ios_Swift_Uicollectionview_Uiimageview_Selector - Fatal编程技术网

Ios 无法识别的选择器发送到UIButton中的实例

Ios 无法识别的选择器发送到UIButton中的实例,ios,swift,uicollectionview,uiimageview,selector,Ios,Swift,Uicollectionview,Uiimageview,Selector,我在里面有一个集合视图和图像视图,我添加了一个ui按钮,以在选择后删除图像。当我单击该按钮时,它崩溃并出现以下错误: AdPostViewController deleteUser]:发送到实例0x7fb588d5b7f0的选择器无法识别 为什么会发生这种情况?我该如何解决?这是我的密码: func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionView

我在里面有一个集合视图和图像视图,我添加了一个
ui按钮
,以在选择后删除图像。当我单击该按钮时,它崩溃并出现以下错误:

AdPostViewController deleteUser]:发送到实例0x7fb588d5b7f0的选择器无法识别

为什么会发生这种情况?我该如何解决?这是我的密码:

func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“ImageCell”,for:indexPath)作为!ImageCell
设img=self.PhotoArray[indexPath.row]
cell.image.image=img
cell.deleteButton?.layer.setValue(indexath.row,forKey:“索引”)
cell.deleteButton?.addTarget(self,action:Selector((“deleteUser”)),用于:UIControl.Event.touchUpInside)
返回单元
}
func deleteUser(u发送方:ui按钮){
设i:Int=(sender.layer.value(forKey:“index”))为!Int
光阵列。移除(at:i)
//PhotoArray.removeAtIndex(i)
ImagesCollectionView.reloadData()
}

一个问题是,您强制手动生成Objective-C选择器,而实际上您不知道如何手动生成Objective-C选择器,因此您弄错了。不要那样做!让编译器为您形成选择器。那是它的工作。替换

action: Selector(("deleteUser"))

此外,还需要将
deleteUser
方法显式公开给Objective-C:

@objc func deleteUser(_ sender: UIButton) {

否则Objective-C仍然无法反思您的类并在调用它时找到此方法。幸运的是,当您切换到
#selector
语法时,编译器将为您指出该问题

这不应该是选择器(deleteUser(:),因为他的选择器有一个匿名参数吗?@duncac否,没有歧义,所以基名称可以作为此函数的引用。这就是选择器的乐趣所在。它认为你不必这么做。
@objc func deleteUser(_ sender: UIButton) {