1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
class
ViewController
:
UIViewController
{
@IBOutlet
var
webview:
UIWebView
!
@IBOutlet
var
loadtype:
UISegmentedControl
!
override
func
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//默認選中分段控件的第一項
loadtype.selectedSegmentIndex = 0
typeChanged(loadtype)
}
@IBAction
func
typeChanged(sender:
UISegmentedControl
)
{
var
index = sender.selectedSegmentIndex
println
(index)
switch
index
{
case
0:
//在 UIWebView 中顯示 HTML 內容
webview.loadHTMLString(html,baseURL:
nil
)
case
1:
//在 UIWebView 中顯示 PDF
var
path =
NSBundle
.mainBundle().pathForResource(
"test1"
, ofType:
"pdf"
)
var
urlStr =
NSURL
.fileURLWithPath(path!);
println
(urlStr)
webview.loadRequest(
NSURLRequest
(
URL
:urlStr!));
case
2:
//在 UIWebView 中顯示 PDF, 可是是經過 loadData 方式加載
var
path =
NSBundle
.mainBundle().pathForResource(
"test1"
, ofType:
"pdf"
)
var
urlStr =
NSURL
.fileURLWithPath(path!);
var
data =
NSData
(contentsOfURL:urlStr!);
webview.loadData(data,
MIMEType
:
"application/pdf"
,
textEncodingName:
"utf-8"
,baseURL:
nil
);
default
:
println
(
"是否是出錯了?"
)
}
}
}
|