ios - 将目标添加到 Collection View Nib 内的按钮操作

标签 ios swift uicollectionview custom-keyboard openurl

我在 iOS 11 中有一个键盘扩展,其中包含来自 JSON 的文章的 Collection View 。我在原型(prototype)单元格中有一个按钮,我想让用户按下它以在键盘外部的 Safari 中打开文章。我可以让它打开静态 URL 中的所有链接,但我无法让它打开每篇文章的 URL。我错过了什么?

我已经给出了一个工作简单的静态 Action 的例子,还包括了我已经尝试过但在这段代码中不起作用的东西:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if(collectionView == self.key.colImages)
        {


            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gifCollectionViewCell", for: indexPath) as! gifCollectionViewCell
            cell.lblTitle.text = self.articles[indexPath.row].headline
            let prefix: String = "https://res.cloudinary.com/djvbbwrnm/image/fetch/"
            let options: String = "w_0.2/"
            if let imageURL =  self.articles[indexPath.row].imageURL
            {

                let articleURL = self.articles[indexPath.row].url
                let url = URL(string: articleURL!)
                let urlAppended = prefix+options+imageURL
                cell.imgView.sd_setImage(with: URL(string: urlAppended), completed: nil)

                //This works 
                cell.shareButton.addTarget(self, action: #selector(openLink), for: .touchUpInside)

                //This doesn't
                cell.shareButton.addTarget(self, action: #selector(openUrl(url: url)), for: .touchUpInside)

            }

            return cell
        }
        else
        {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "catCollectionViewCell", for: indexPath) as! catCollectionViewCell
            cell.imgView.image = buttPics[indexPath.row]
            cell.imgView.layer.cornerRadius = 2
            cell.imgView.layer.masksToBounds = true
        return cell
        }

    }

      @objc func openLink(){
        let articleURL = "http://google.com"
        let url = URL(string: articleURL)
        openUrl(url: url)

    }

       @objc func openUrl(url: URL?) {
        let selector = sel_registerName("openURL:")
        var responder = self as UIResponder?
        while let r = responder, !r.responds(to: selector) {
            responder = r.next
        }
        _ = responder?.perform(selector, with: url)
    }

最佳答案

您不能添加任何其他 DataTypes 作为参数。因为,您正在为 UIButton 添加 addTarget。

@objc func openLink(){

}

@objc func openLink(sender: UIButton){ // URL is not possible.

}

以上两段代码相同。在第二个中,您可以访问 UIButton 的 property

可运行代码

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if(collectionView == self.key.colImages)
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gifCollectionViewCell", for: indexPath) as! gifCollectionViewCell
        cell.lblTitle.text = self.articles[indexPath.row].headline
        let prefix: String = "https://res.cloudinary.com/djvbbwrnm/image/fetch/"
        let options: String = "w_0.2/"
        if let imageURL =  self.articles[indexPath.row].imageURL
        {

            //let articleURL = self.articles[indexPath.row].url
            //let url = URL(string: articleURL!)
            let urlAppended = prefix+options+imageURL
            cell.imgView.sd_setImage(with: URL(string: urlAppended), completed: nil)

            //This works 
            cell.shareButton.addTarget(self, action: #selector(openLink), for: .touchUpInside)

            //This doesn't
            //cell.shareButton.addTarget(self, action: #selector(openUrl(url: url)), for: .touchUpInside)

            cell.shareButton.tag = indexPath.row // SET TAG TO UIBUTTON

        }

        return cell
    }
    else
    {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "catCollectionViewCell", for: indexPath) as! catCollectionViewCell
        cell.imgView.image = buttPics[indexPath.row]
        cell.imgView.layer.cornerRadius = 2
        cell.imgView.layer.masksToBounds = true
    return cell
    }

}


@objc func openLink(sender: UIButton){ // USE THIS.

     let buttonTag : Int = sender.tag
     let articleURL = self.articles[buttonTag].url
     let url = URL(string: articleURL!)
     // You can achieve by this way.

    // Since I am in a keyboard extension, I added the follwoing code and it is working now.
   let selector = sel_registerName("openURL:")
    var responder = self as UIResponder?
    while let r = responder, !r.responds(to: selector) {
        responder = r.next
    }
    _ = responder?.perform(selector, with: url)
}

关于ios - 将目标添加到 Collection View Nib 内的按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621206/

相关文章:

ios - RFQuiltLayout 和 UICollectionView

iOS:UICollectionView 单元格选择不起作用

ios - iOS 中的 GPUImage 二值化

ios - WebP 编码的数据加载时间超过 30 秒

ios - 将 Swift 闭包作为值添加到 Swift 字典

swift - 如何创建运算符以实现错误链接?

iOS - 异步请求和 UI 交互

ios - 是否有可能有一个 SCNNode 是透明的,但它遮挡了它后面的任何对象?

ios - 更新到 Swift 1.2 后出错

ios - 快速获取错误的设备语言