简体   繁体   English

在集合视图的笔尖内将addTarget更改为按钮动作

[英]addTarget to button action within nib of collection view

I have a keyboard extension in iOS 11 that includes a collection view of articles coming in from JSON. 我在iOS 11中有一个键盘扩展程序,其中包括来自JSON的文章的集合视图。 I have a button in the prototype cell that I would like to allow a user to press to open the article in Safari external to the keyboard. 我在原型单元格中有一个按钮,我希望允许用户按下该按钮以在键盘外部的Safari中打开文章。 I can get it to open all links in a static URL, but I cant get it to open each article's URL. 我可以用它打开静态URL中的所有链接,但是我不能用它打开每篇文章的URL。 What am I missing? 我想念什么?

I've put an example of the working simple static action and also included what I have tried but doesn't work in this code: 我举了一个简单的静态动作示例,还包括了我尝试过但在此代码中不起作用的内容:

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)
    }

You cant add any other DataTypes as arguments. 您不能添加任何其他数据DataTypes作为参数。 Because, you are adding addTarget for UIButton. 因为,您要为UIButton添加addTarget。

@objc func openLink(){

}

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

}

The above two codes are same. 以上两个代码是相同的。 In second one, you can access that UIButton's property . 在第二篇中,您可以访问UIButton's property

Runnable Code 可运行代码

  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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM