关于sender.tag按钮

时间:2017-12-19 00:57:22

标签: ios swift

我对CellViewConnection有疑问。我有两个故事板:

  • 第一个故事板是CellViewConnection,带有按钮和PDF filesList item,
  • 第二个用于阅读PDF文件。

但我有一个问题。当我按下第一个按钮向我显示带有sender.tag的第一个PDF文件时,我创建了代码。但是我没有得到第一个按钮的结果,第二个按钮得到的结果与第一个按钮相同。

如何让第一个按钮获取第一个按钮,第二个按钮获取第二个PDF文件?
这是我的代码:

import UIKit
import PDFKit

@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
@available(iOS 11.0, *)
class FirstARViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {


    @IBOutlet weak var collectionView: UICollectionView!
    var imagescv = ["ar1","ar2" ]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.delegate = self
        collectionView.dataSource = self
    }




    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagescv.count
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! cellimagesar

        cell.layer.cornerRadius = 5

        cell.layer.borderWidth = 1

        cell.myImages.image = UIImage(named: imagescv [indexPath.row])
        cell.myImages.contentMode = .scaleToFill


        cell.buttonMove.tag = indexPath.item
        cell.buttonMove.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)

        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 171, height: 250)
    }
    let me = PDFViewController()
    @objc func buttonClicked(_ sender: UIButton) {
        if (sender.tag == 0) {
            PDFimport()
            let pdfView = PDFView(frame: UIScreen.main.bounds)

            let url = Bundle.main.url(forResource: "AR1", withExtension: "pdf")

            let vc = UIDocumentInteractionController(url: url!)
            pdfView.document = PDFDocument(url: url!)
            view.addSubview(pdfView)
            vc.delegate = (PDFViewController() as UIDocumentInteractionControllerDelegate)
                  }

       else if (sender.tag == 1) {
            PDFimport()
            let pdfView = PDFView(frame: UIScreen.main.bounds)

            let url = Bundle.main.url(forResource: "AR2", withExtension: "pdf")

            let vc = UIDocumentInteractionController(url: url!)
            pdfView.document = PDFDocument(url: url!)
            view.addSubview(pdfView)
            vc.delegate = (PDFViewController() as UIDocumentInteractionControllerDelegate)
        }

    }

    func PDFimport()
    {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PDF") 
        self.present(nextViewController, animated:true, completion:nil)
    }
    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

        return self
    }

}

第二视图:

import UIKit
import PDFKit
import QuickLook

@available(iOS 11.0, *)
@available(iOS 11.0, *)

class PDFViewController: UIViewController {

    lazy var document: UIDocumentInteractionController = {
        let pdfView = PDFView(frame: UIScreen.main.bounds)
        let url = Bundle.main.url(forResource: "", withExtension: "pdf")

        let vc = UIDocumentInteractionController(url: url!)
        pdfView.document = PDFDocument(url: url!)
        view.addSubview(pdfView)
        vc.delegate = (PDFViewController() as UIDocumentInteractionControllerDelegate)

        return vc

    }()

    override func viewDidLoad() {
        super.viewDidLoad()


       document.presentPreview(animated: true)



    }





    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    @IBOutlet weak var pdfView: PDFView!



    @IBAction func doAction1(_ sender: AnyObject) {

       document.presentOpenInMenu(from: view.bounds, in: view, animated: true)
    }




    @IBAction func save(_ sender: Any)
    {
       document.presentOptionsMenu(from: view.bounds, in: view, animated: true)


    }



    @IBAction func backbutton(_ sender: Any)
    {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "back")
        self.present(nextViewController, animated:true, completion:nil)
    }



}

@available(iOS 11.0, *)
extension PDFViewController: UIDocumentInteractionControllerDelegate {


    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }

}

2 个答案:

答案 0 :(得分:2)

这里我使用了相同的代码

我的CollectionView cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        //Declaring cell 
        let cell =     collectionView.dequeueReusableCell(withReuseIdentifier:              cellReuseIdentifier,for: indexPath) as! CustomCollectionViewCell

        //Adding Button reference No need to give Tags Differently 
        cell.StarImageButton.addTarget(self, action: #selector(HomeVC.FavouriteButtonHandler(sender:)), for: .touchUpInside)

        //Return cell
        return cell
    }

我的按钮处理程序

@objc func FavouriteButtonHandler (sender: UIButton)
    {
        //Lets get the button position as CGPoint
        let buttonPosition : CGPoint = sender.convert(CGPoint.zero, to: self.HotDealsCollectionView)

        //using CGPoint we can get index Path of cell in which button was clicked
        let indexPath : IndexPath = self.HotDealsCollectionView.indexPathForItem(at: buttonPosition)!

        //Now, Do any required operation as here I need to reload my collectionView cell 
        self.HotDealsCollectionView.reloadItems(at: [indexPath])

        //You can use the index path to iterate in array of pdf files like 
        **pdfArray[indexPath.row]** //And open it

        //as per in your case
       switch indexPath.row {
       case 0:
           //Required Code for opening Pdf at index 0
           break
        case 1:
           //Required Code for opening Pdf at index 1
           break
        default:
           print("No index FOund")
        } 
    }

这里我实际上是指而不是使用按钮标签为什么不直接使用所选索引,就像我们通常在cellForItemAt中做的那样

答案 1 :(得分:0)

我遇到了你的问题......

cell.buttonMove.tag = indexPath.item
cell.buttonMove.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)

现在如果您分析代码,您可以看到您正在调用没有参数的函数,这意味着UIButton永远不会分配给您的函数参数,这最终会导致问题,默认情况下您的标记始终为零,这就是为什么它总是打开你的第一个pdf文件。

请更改您的代码

cell.buttonMove.tag = indexPath.item
cell.buttonMove.addTarget(self, action: #selector(self.buttonClicked(_:)), for: .touchUpInside)

有关详细信息,请查看此answer