我对CellViewConnection有个问题。我有两个故事板:
但我有个问题。当我按下第一个按钮显示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
}
}发布于 2017-12-19 04:35:38
在这里,我使用了与您使用的相同的代码
我的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中那样直接使用选定的索引,而不是使用按钮标记
发布于 2017-12-19 04:45:35
我有你的问题..。
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
https://stackoverflow.com/questions/47878611
复制相似问题