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.
我有一个视图控制器,它包含两个容器。 First Container嵌入了FirstViewController,它有2个按钮,第二个Container嵌入了带有webview的PreviewViewController。我想在该Webview中加载pdf。我已经采取了webview的出路。在ViewDidLoad上隐藏Webview。现在我希望如果我点击任何显示webview sholud的按钮,我点击Button1 webview应该加载名为1的PDF并通过点击Button2 webview应该加载名为2的PDF。
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
在解开Optional值时意外地发现了nil
on Line webview.hidden = true
in the code which is given below. This is written in PreviewViewController
.
在线webview.hidden = true在下面给出的代码中。这是在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.
现在,我在buttonclick上从FirstViewController调用此函数。
PreviewViewController().showpdf(1)
But app crashes by giving error given above. Can anyone Suggest me how can I solve this?
但是app通过给出上面给出的错误而崩溃了。任何人都可以建议我如何解决这个问题?
EDIT: I am calling it on button click of FirstViewController's Button Click.
编辑:我按下FirstViewController的按钮单击按钮调用它。
@IBAction func btnSave(sender: UIButton) {
PreviewViewController().showpdf(5)
}
Thanks.
1 个解决方案
#1
0
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.
在我为您的问题解释一个可能的解决方案之前,我强烈建议您完成iOS MVC的完整课程,可能需要几天时间,但您可以自己轻松解决这类问题。来自斯坦福大学的Hegarty教授在互联网上有一个适用于iOS8的版本。虽然swift已经从iOS 8改变了,但MVC架构并没有改变。
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)
你需要的是创建一个协议,用作按下两个按钮的动作的委托,让我们称之为PDFProtocol。该协议应该有1个方法,例如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:
您的主视图控制器应实现PDFProtocol。现在,准备segue的主视图控制器应该将自己设置为第一个视图控制器的pdfDelegate,它应该保留指向预览视图控制器的指针,如下所示:
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:
第一个视图控制器应该有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:
然后,主视图控制器应该实现showPDF(id:String)方法,如下所示:
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!
就是这样。我直接在这里输入代码而不试图编译它,所以要小心拼写错误!
#1
0
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.
在我为您的问题解释一个可能的解决方案之前,我强烈建议您完成iOS MVC的完整课程,可能需要几天时间,但您可以自己轻松解决这类问题。来自斯坦福大学的Hegarty教授在互联网上有一个适用于iOS8的版本。虽然swift已经从iOS 8改变了,但MVC架构并没有改变。
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)
你需要的是创建一个协议,用作按下两个按钮的动作的委托,让我们称之为PDFProtocol。该协议应该有1个方法,例如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:
您的主视图控制器应实现PDFProtocol。现在,准备segue的主视图控制器应该将自己设置为第一个视图控制器的pdfDelegate,它应该保留指向预览视图控制器的指针,如下所示:
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:
第一个视图控制器应该有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:
然后,主视图控制器应该实现showPDF(id:String)方法,如下所示:
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!
就是这样。我直接在这里输入代码而不试图编译它,所以要小心拼写错误!