关于sender.tag按钮

问题描述 投票:1回答:2

我有CellViewConnection的问题。我有两个故事板:

  • 第一个故事板是CellViewConnection带按钮和PDF文件列表项,
  • 第二个是阅读PDF文件。

但我有一个问题。当我按下第一个按钮向我显示第一个带有sender.tag的PDF文件时,我制作了代码。但是我没有得到第一个按钮的结果,第二个按钮我得到的结果与第一个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
    }

}
ios swift
2个回答
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中所做的那样


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

© www.soinside.com 2019 - 2024. All rights reserved.