Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Swift 将目标添加到按钮到附件视图_Swift_Button_Tableview_Target_Accessoryview - Fatal编程技术网

Swift 将目标添加到按钮到附件视图

Swift 将目标添加到按钮到附件视图,swift,button,tableview,target,accessoryview,Swift,Button,Tableview,Target,Accessoryview,我已经创建了一个按钮,并已将其分配给我的accessoryView。我可以看到创建的按钮,但当我按下它时,什么也没有发生。有人能看出我做错了什么吗 let accessoryButton = UIButton(type: .roundedRect) accessoryButton.setTitle("message", for: .normal) accessoryButton.sizeToFit() accessoryButton.addTarget(self,

我已经创建了一个按钮,并已将其分配给我的accessoryView。我可以看到创建的按钮,但当我按下它时,什么也没有发生。有人能看出我做错了什么吗

   let accessoryButton = UIButton(type: .roundedRect)
    accessoryButton.setTitle("message", for: .normal)
    accessoryButton.sizeToFit()

    accessoryButton.addTarget(self, action: #selector(goToMessaging(sender:)), for: UIControlEvents.touchUpInside)

    cell.accessoryView = accessoryButton as UIView



 @objc func goToMessaging(sender: UIButton){
    print("message him")
}

可能是因为您正在将按钮投射到UIView

accessoryButton as UIView
如果必须是UIView,您可以尝试将点击手势识别器添加到accessoryView

试试这个

@objc func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //let cell = ...
        cell.accessoryType = UITableViewCellAccessoryType.detailButton
        cell.tintColor = .red
}

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {

        print("accessory Button Tapped = \(indexPath.row)")
}

不,这没用。我尝试使用手势识别器将其转换为按钮和UIView,但没有成功。您在哪里初始化按钮?看起来您正在
cellforrowatinexpath
中创建按钮。是的,我就是在那里创建的