Swift - UIControl addTarget无效

时间:2017-06-14 10:15:53

标签: ios uicollectionview uicontrol

我用UIView创建了XIB。我已经为其分配了课程MyViewControl: UIControl,因此我可以添加内部动作。它有效,触摸在我的课堂内调用。

现在,我创建了UICollectionView并添加了这个" xib"作为细胞视图。我现在想要在UICollectionViewDelegate类中处理。所以我这样做了:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {        
    for v in cell.contentView.subviews {            
        if let c: MyViewControl = v as? MyViewControl {               
            c.isUserInteractionEnabled = true
            c.addTarget(self, action: #selector(myClick), for: UIControlEvents.touchUpInside)
        }
    }        
}

func myClick(sender: Any){
    print("click....")
}

但是,永远不会调用myClick选择器。如果我将UIButton添加到MyViewControl并为此按钮执行相同操作(addTarget),则会正确调用回调。

1 个答案:

答案 0 :(得分:1)

在发送方之前添加下划线_,将_ sender: Any作为(_:)添加#selector下的actionmyClick(_ :)func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { for v in cell.contentView.subviews { if let c: MyViewControl = v as? MyViewControl { c.isUserInteractionEnabled = true c.addTarget(self, action: #selector(myClick(_:)), for: UIControlEvents.touchUpInside) } } } func myClick(_ sender: Any){ print("click....") }

function FileUploadCheck() {
var formData = new FormData();
formData.append('LogoImageUploader', jQuery('#LogoImageUploader')[0].files[0]);

    var http = new XMLHttpRequest();
    var url = "FileUploadChecker.php";

    http.open("POST", url, true);

    //when i uncommentated this line, the upload failed, as i tried to manually set the header to the post, but i let XHR to set the header automatically this works.   
    //http.setRequestHeader("Content-type", "multipart/form-data");



    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(formData);
}

希望这有帮助