关闭 pdf 文件后故事板 segue 错误

Posted

技术标签:

【中文标题】关闭 pdf 文件后故事板 segue 错误【英文标题】:storyboard segue error after closing pdf file 【发布时间】:2017-01-11 19:55:54 【问题描述】:

当我在完成后加载 pdf 文件时,当我按下完成时,它应该返回到上一个视图控制器,如果我愿意,我应该能够再次打开相同的 pdf 文件。但是当我尝试打开相同的pdf时,情况并非如此,我得到了这个错误

fileSystemRepresentationWithPath:]: nil 或空路径参数'

如果我将关闭功能更改为此 _ = self.navigationController?.popToRootViewController(animated: true)

它仍然可以工作。谁能帮我解决这个问题。这是我的代码。

类 showA3lamDa3waVC: UIViewController , UIWebViewDelegate,ReaderViewControllerDelegate

var pdfbooks = UIWebView()
var nIndex:NSInteger!
var post: Post!
var db : DBHelperA3lam = DBHelperA3lam()
var book : a3lamDa3waModel?


@IBAction func backbtn(_ sender: Any) 

    if let navController = self.navigationController 
        navController.popViewController(animated: true)
    



override func viewDidLoad() 
    super.viewDidLoad()

    var config : SwiftLoader.Config = SwiftLoader.Config()
    config.size = 150
    config.spinnerColor = .brown
    config.foregroundColor = .black
    config.foregroundAlpha = 0.5
    config.titleTextColor = .brown


    SwiftLoader.setConfig(config)


    if "" !=  book?.bookPath 
     //   self.activityIND.isHidden = true
     //   self.activityIND.stopAnimating()

        SwiftLoader.hide()
        UIApplication.shared.isNetworkActivityIndicatorVisible = false

        loadReader(filePaht: (book?.bookPath)!)
     else 

        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let strName = book?.id
        let filePath = "\(documentsPath)/"+strName!+".pdf"
        let fileManager = FileManager.default
    //    self.activityIND.startAnimating()

        SwiftLoader.show(title: "Loading...", animated: true)
        UIApplication.shared.isNetworkActivityIndicatorVisible = true


        if fileManager.fileExists(atPath: filePath) 
            //    self.loadFromUrl(path: filePath)
            loadReader(filePaht: (book?.bookPath)!)
            return;
        


        let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!)
        reference.data(withMaxSize: 50 * 1024 * 1024)  (data, error) -> Void in
            if (error != nil) 

                print ("unable to download pdf file from Firebase Storage")

           //     self.activityIND.isHidden = false
           //     self.activityIND.startAnimating()
                SwiftLoader.show(title: "Loading...", animated: true)
                UIApplication.shared.isNetworkActivityIndicatorVisible = true


             else 

                if ((try! data?.write(to: URL.init(fileURLWithPath: filePath, isDirectory: false))) != nil) 
                    //       self.loadFromUrl(path: filePath)
                    print ("pdf file is downloaded from Firebase Storage")
                    self.db.upDate(id: (self.book?.id)!, bookPath: filePath)
                 //   self.activityIND.isHidden = true
                    SwiftLoader.hide()
                    self.loadReader(filePaht: filePath)
                    UIApplication.shared.isNetworkActivityIndicatorVisible = false




                
            
        

    



func loadReader(filePaht : String)  

    let document = ReaderDocument(filePath: filePaht, password: nil)
    if document != nil 
        let readerVC = ReaderViewController(readerDocument: document)
        readerVC?.delegate = self
        readerVC?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
        readerVC?.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        self.navigationController?.pushViewController(readerVC!, animated: true)
    



func dismiss(_ viewController: ReaderViewController!) 

   // _ = self.navigationController?.popToRootViewController(animated: true)

    if let a3lamDa3waVC = self.navigationController?.viewControllers[1] 

     _ = navigationController?.popToViewController(a3lamDa3waVC, animated: true)
          



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()

这里是集合视图的代码

a3lamDa3waVC 类:UIViewController、UICollectionViewDelegate、UICollectionViewDataSource

@IBOutlet weak var collectionView: UICollectionView!


var posts = [Post]()
let db : DBHelperA3lam = DBHelperA3lam()
var arrayBooks = [a3lamDa3waModel]()
var selectedIndexPath : IndexPath = IndexPath()
var post: Post!

override func viewDidLoad() 

    var config : SwiftLoader.Config = SwiftLoader.Config()
    config.size = 150
    config.spinnerColor = .brown
    config.foregroundColor = .black
    config.foregroundAlpha = 0.5
    config.titleTextColor = .brown


    SwiftLoader.setConfig(config)


    super.viewDidLoad()
 //   activityIndicator.isHidden = false

    SwiftLoader.show(title: "Loading...", animated: true)


    self.arrayBooks.removeAll()
    self.arrayBooks.append(contentsOf: self.db.fetchAll())
    self.collectionView.reloadData()
    DataService.ds.REF_POSTS.observe(.value, with:  (snapshot) in


        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] 

            for snap in snapshot 
                print ("SNAP: \(snap)")

                if let postDict = snap.value as? Dictionary<String, AnyObject>
                    let key = snap.key
                    let post = Post(postKey: key , postData: postDict)
                    self.posts.append(post)
                    self.db.insertBook(id: postDict["id"] as! String, imgName: postDict["id"] as! String, imgPath: "", bookName: postDict["book_name"] as! String, bookPath: "", imageURL: postDict["image_path"] as! String, bookURL: postDict["book_path"] as! String, caption: "")




                
            
         else 

        
        self.arrayBooks.removeAll()
        self.arrayBooks.append(contentsOf: self.db.fetchAll())
        self.collectionView.reloadData()

    )


    collectionView.delegate = self
    collectionView.dataSource = self


func getFilePath(name : String) -> String 

    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let filePath = documentsPath+"/"+name
    return filePath



@IBAction func backbtn(_ sender: Any) 
    if let navController = self.navigationController 
        navController.popViewController(animated: true)
    




func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 


    return arrayBooks.count

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

    let book = arrayBooks[indexPath.item]
     SwiftLoader.hide()

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as? CollectionViewCellA3lamDa3wa 

        cell.initWithBook(book: book)

        return cell

    else 
        return CollectionViewCellA3lamDa3wa()
    




func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    self.selectedIndexPath = indexPath
    self.performSegue(withIdentifier: "showImage", sender: self)


override func prepare(for segue: UIStoryboardSegue, sender: Any?)


    if segue.identifier == "showImage"
    
        let vc = segue.destination as! showA3lamDa3waVC
        vc.book = self.arrayBooks[self.selectedIndexPath.row]

    

【问题讨论】:

在 viewwillappear 方法中添加 viewDidLoad 代码。希望它会工作 我试过这样做,但没有成功 为什么你在当前控制器而不是 ReaderViewController 中调用了解除方法。如果我没记错的话,你可以直接从 ReaderViewController 中解散。 【参考方案1】:

为什么不使用popViewController(animated:) 方法?它必须回到前一个视图控制器 (Apple doc.)。

【讨论】:

当我使用它时,它会返回但集合视图不显示。 当你回到 vc 时,arrayBooks 是空的吗? 不,如果我再次尝试打开同一个 pdf 文件,它会给我一个错误 如果在关闭函数中我使用这个 _ = self.navigationController?.popToRootViewController(animated: true) 它工作正常,但这不是我想要的。我只想回到vc

以上是关于关闭 pdf 文件后故事板 segue 错误的主要内容,如果未能解决你的问题,请参考以下文章

带有程序故事板segue的NSInternalInconsistencyException

故事板 Segue 未加载视图

在没有 Segues 的故事板之间传递变量 - Swift

在没有Segues的故事板之间传递变量 - Swift

在故事板xcode中直接从视图控制器警告启动的Segues

在多个故事板环境中展开 segue