我正在构建一个具有PDF本地的应用程序,可以脱机使用。我使用ENSideMenu和表视图动态指向并在UIWebView中显示PDF。以下是PDF视图的代码:
var pdfLoc = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("PDF_file", ofType:"pdf")!) //replace PDF_file with your pdf die name
var request = NSURLRequest(URL: pdfLoc);
self.pdfWebView.loadRequest(request);
这是我的导航控制器:
import UIKit
class MyNavigationController: ENSideMenuNavigationController, ENSideMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
sideMenu = ENSideMenu(sourceView: self.view, menuTableViewController: MyMenuTableViewController(), menuPosition:.Left)
sideMenu?.delegate = self //optional
sideMenu?.menuWidth = 180.0 // optional, default is 160
//sideMenu?.bouncingEnabled = false
// make navigation bar showing over side menu
view.bringSubviewToFront(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
println("sideMenuWillOpen")
}
func sideMenuWillClose() {
println("sideMenuWillClose")
}
这是我的表视图控制器:
import UIKit
import QuickLook
class FirstStepsTableViewController: UITableViewController {
var pdfs = [FirstStep]()
override func viewDidLoad() {
super.viewDidLoad()
var newPdf = FirstStep(name: "First People To Call List", filename: "firstpeople")
pdfs.append(newPdf)
newPdf = FirstStep(name: "Top 5 Things To Do In An Emergency", filename: "top5")
pdfs.append(newPdf)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pdfs.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("pdfCell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
var currentFirstSteps = pdfs[indexPath.row]
cell.textLabel?.text = currentFirstSteps.name
cell.textLabel?.numberOfLines = 0;
// Retrieve an image
var myImage = UIImage(named: "pdf")
cell.imageView?.image = myImage
return cell
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "First Step"
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
return 50
}
var secondScene = segue.destinationViewController as DisplayFirstStepsViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow() {
let selectedPdf = pdfs[indexPath.row]
secondScene.currentFirstSteps = selectedPdf
}
}
}
这是我在UIViewController中的UIWebView:
import UIKit
class DisplayFirstStepsViewController: UIViewController {
@IBOutlet weak var currentDocument: UIWebView!
var currentFirstSteps : FirstStep?
override func viewDidLoad() {
super.viewDidLoad()
var pdfLoc = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource((named: currentFirstSteps!.filename), ofType:"pdf")!)
var request = NSURLRequest(URL: pdfLoc!);
self.currentDocument.loadRequest(request);
navigationController?.hidesBarsOnSwipe = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的MenuTableViewController:
import UIKit
class MyMenuTableViewController: UITableViewController {
var eachSection = ["Home","First Steps","Communication","Equipment & Supplies", "Computer Systems","Critical Vendor Contact Info"]
var selectedMenuItem : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Customize apperance of table view
tableView.contentInset = UIEdgeInsetsMake(64.0, 0, 0, 0) //
tableView.separatorStyle = .None
tableView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)
tableView.scrollsToTop = false
// Preserve selection between presentations
self.clearsSelectionOnViewWillAppear = false
tableView.selectRowAtIndexPath(NSIndexPath(forRow: selectedMenuItem, inSection: 0), animated: false, scrollPosition: .Middle)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return 6
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
cell!.backgroundColor = UIColor.clearColor()
cell!.textLabel?.textColor = UIColor.whiteColor()
cell!.textLabel?.numberOfLines = 0;
let selectedBackgroundView = UIView(frame: CGRectMake(0, 0, cell!.frame.size.width, cell!.frame.size.height))
selectedBackgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.4)
cell!.selectedBackgroundView = selectedBackgroundView
}
cell!.textLabel?.text = eachSection[indexPath.row]
return cell!
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50.0
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("did select row: \(indexPath.row)")
if (indexPath.row == selectedMenuItem) {
return
}
selectedMenuItem = indexPath.row
//Present new view controller
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var destViewController : UIViewController
switch (indexPath.row) {
case 0:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as UIViewController
break
case 1:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("FirstSteps") as UIViewController
break
case 2:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Emergency") as UIViewController
break
case 3:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("EmergencyEquip") as UIViewController
break
case 4:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Computer") as UIViewController
break
case 5:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Critical") as UIViewController
break
default:
destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as UIViewController
break
}
sideMenuController()?.setContentViewController(destViewController)
}
我的问题是菜单加载,我点击“页面”(表格视图控制器),然后点击表格视图单元格,然后将其带到带有PDF的UIWebView的UIViewController。当我点击返回表格时,菜单不再有效。我收到以下错误:
无法解析旧目录的绝对路径。
任何想法如何解决这个问题?在此先感谢!