简体   繁体   中英

Calling Function of one class from another class Swift

I have one view controller and it contains two Containers. First Container is embedded with FirstViewController which has 2 buttons and second Container is embedded with PreviewViewController which has webview. I want to load pdf in that Webview. I have taken outlet of webview. on ViewDidLoad Webview is hidden. Now I want that If I clicked on any button that webview sholud be shown and I click Button1 webview should load PDF named 1 and by clicking on Button2 webview should load PDF named 2.

Now I don't know to do this!! This is my code. It is giving me an error of

unexpectedly found nil while unwrapping an Optional value

on Line webview.hidden = true in the code which is given below. This is written in PreviewViewController .

@IBOutlet var webview: UIWebView!

func showpdf(id : Int)
    {
        webview.hidden = false

        if id == 1
        {
            if let pdf = NSBundle.mainBundle().URLForResource("1", withExtension: "pdf", subdirectory: nil, localization: nil)  {
                let req = NSURLRequest(URL: pdf)
                // let webView = UIWebView(frame: CGRectMake(30,30,self.view.frame.size.width-250,self.view.frame.size.height-250))
                webview.loadRequest(req)
                self.view.addSubview(webview)
            }
        }
        else if id == 2
        {
            if let pdf = NSBundle.mainBundle().URLForResource("2", withExtension: "pdf", subdirectory: nil, localization: nil)  {
                let req = NSURLRequest(URL: pdf)
                webview.loadRequest(req)
                self.view.addSubview(webview)
            }
        }        
    }

Now, I am calling this Function from FirstViewController on buttonclick.

PreviewViewController().showpdf(1)

But app crashes by giving error given above. Can anyone Suggest me how can I solve this?

EDIT: I am calling it on button click of FirstViewController's Button Click.

@IBAction func btnSave(sender: UIButton) {

       PreviewViewController().showpdf(5)
 }

图片

Thanks.

Before I explain one possible solution to your problem, I strongly advise you to go through a complete course on iOS MVC, it might take a few days but you will be able to get around this kind of problem easily on your own. There is one available for iOS8 by Prof. Hegarty from Stanford on the internet. Although swift has changed from what it was in iOS 8 the MVC architecture did not change.

What you need is to create a protocol to use as delegate for the action of pressing the two buttons, lets call it PDFProtocol . This protocol should have 1 method, for example showPDF(id:String)

Your main view controller should implement PDFProtocol . Now the main view controller in the prepare for segue should set itself as pdfDelegate for the first view controller and it should keep a pointer to the preview view controller Like this:

if let t = targetViewController as? FirstViewController {
    t.pdfDelegate = self
} else if let t = targetViewController as? PreviewViewController {
    self.previewViewController = t
}

The first view controller should have a variable for the pdfDelegate:

var pdfDelegate: PDFProtocol?

and when you press the button should call the appropriate action on the delegate like this:

guard delegate = pdfDelegate else {
    AssertFail("no delegate found")
    return
}
delegate.showPDF("myfirstpdf")

The main view controller should then implement the showPDF(id:String) method as follows:

func showPDF(id:string) {
    self.previewViewController.showPDF(id)
}  

And that's it. I typed the code directly in here without trying to compile it, so beware of typos!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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