在辅助viewcontroller上显示来自网站的pdf

时间:2018-03-03 01:13:57

标签: ios swift xcode listview uiwebview

我的应用程序中有多个列表,我现在希望在用户单击一个单元格时从网站显示pdf。我很好奇我可以在listview代码中实现哪些代码将信息传递给辅助控制器,辅助控制器将在UIWebView(?)而不是图像中显示pdf。

我的代码ID就像编辑以发送pdf而不是列表中的图像:

import UIKit
import GoogleMobileAds


class tablelist: UIViewController, UITableViewDataSource, UITableViewDelegate, GADBannerViewDelegate {

    var bannerView: GADBannerView!


     override func viewDidLoad(){
     super.viewDidLoad()

        self.navigationController?.navigationBar.barTintColor = UIColor.black
        let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
        navigationController?.navigationBar.titleTextAttributes = textAttributes




        self.animalTableView.dataSource = self
        self.animalTableView.delegate = self
        self.view.backgroundColor = UIColor.black


    //--------------------------LIST STUFF-------------------------------------------------------//

    let respiratory = ["Cat", "Dog", "Cow", "Mulval"]



    @IBOutlet weak var animalTableView: UITableView!

    ///Set elements in cell
    func tableView(  _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "adultresplist"/*Identifier*/, for: indexPath)
        cell.textLabel?.text = respiratory[indexPath.row]
    // FONT STYLE
        cell.textLabel?.textColor = UIColor.white
        cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
        cell.textLabel?.textAlignment = .center
        self.animalTableView.backgroundColor = UIColor.clear
        cell.textLabel?.backgroundColor = UIColor.black

        return cell
    }

    ///Return Count

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return respiratory.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {

        let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageViewController") as! imageViewController


        switch indexPath.row
        {
        case 0:
            Vc.passedImage = UIImage.init(named: "image1")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 1:
            Vc.passedImage = UIImage.init(named: "image2")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 2:
            Vc.passedImage = UIImage.init(named: "image3")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 3:
            Vc.passedImage = UIImage.init(named: "image1")!
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        default:
            Vc.passedImage = UIImage.init(named: "image2")!
            self.navigationController?.pushViewController(Vc, animated: true)
        }
   }
}
}

我在辅助控制器中显示图像的代码是:

 class pdfviewer: UIViewController,GADBannerViewDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate, UIWebViewDelegate {

var bannerView: GADBannerView!

var pdfUrl: UIWebView! = nil


@IBOutlet weak var webView: UIWebView!


func loadPdfWithUrl(url: URL)
{
    webView.loadRequest(URLRequest(url: url))
}

func webViewDidFinishLoad(_ webView: UIWebView)
{
    if (webView.isLoading)
    {
        let contentSize:CGSize = webView.scrollView.contentSize
        let viewSize:CGSize = self.view.bounds.size

        let rw : CGFloat = viewSize.width / contentSize.width

        webView.scrollView.minimumZoomScale = rw
        webView.scrollView.maximumZoomScale = rw
        webView.scrollView.zoomScale = rw
    }
}

1 个答案:

答案 0 :(得分:1)

从WebView中的链接加载Pdf的示例代码

在您的TableList中

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let Vc = self.storyboard?.instantiateViewController(withIdentifier: "pdfVC") as! pdfVC

        switch indexPath.row
        {
        case 0:
            Vc.pdfUrl = URL.init(string: "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf")
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 1:
            Vc.pdfUrl = URL.init(string: "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf")
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 2:
            Vc.pdfUrl = URL.init(string: "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf")
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        case 3:
            Vc.pdfUrl = URL.init(string: "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf")
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        default:
            Vc.pdfUrl = URL.init(string: "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf")
            self.navigationController?.pushViewController(Vc, animated: true)
            break;
        }

    }

在pdf控制器中

func loadPdfWithUrl(url: URL)
{
    webView.loadRequest(URLRequest(url: url))
}

func webViewDidFinishLoad(_ webView: UIWebView)
{
    if (webView.isLoading)
    {
        let contentSize:CGSize = webView.scrollView.contentSize
        let viewSize:CGSize = self.view.bounds.size

        let rw : CGFloat = viewSize.width / contentSize.width

        webView.scrollView.minimumZoomScale = rw
        webView.scrollView.maximumZoomScale = rw
        webView.scrollView.zoomScale = rw
    }
}

StoryBoard:

enter image description here

示例代码文件

链接 - https://drive.google.com/open?id=1HmSnGFEGAbPLFdAcUzdbpodAm6gfXbmm