如何将 PFFile 从一个 ViewController 传输到另一个 ViewController

Posted

技术标签:

【中文标题】如何将 PFFile 从一个 ViewController 传输到另一个 ViewController【英文标题】:How Do I Transfer A PFFile From One ViewController To Another 【发布时间】:2015-07-06 03:51:04 【问题描述】:

晚上好。我在将 PFFile 从 TableViewController 传输到 SecondViewController 时遇到问题。我希望用户能够从 tableview 中选择图像,然后编辑他们认为适合下一个视图控制器的视频。

如果我删除了

,我可以选择单元格并转移到 MovieDetailedViewController
if segue.identifier == "viewMe" 
        if var indexPath = self.tableView.indexPathForSelectedRow() 
         var object:PFObject = (self.resultsVideoFiles[indexPath.row] as? PFObject)!
         var file:PFFile = object.objectForKey("video") as! PFFile
        (segue.destinationViewController as! MovieDetailedViewController)
    

    

但是播放了错误的视频。我如何才能播放正确的视频?如果我在 prepareForSegue 函数下保留同一行代码,那么我的程序会出现中断,说无法将类型“PFFile”(0x10c4fff90)的值转换为“PFObject”??

这是我的第一个视图控制器的代码。

import UIKit
import Parse
import ParseUI

class MovieListViewController: UITableViewController 
    var resultsMovienameArray = [String]()
    var resultsImageFiles = [PFFile]()
    var resultsVideoFiles = [PFFile]()
    var video:PFFile!



    override func viewDidLoad() 
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    override func viewDidAppear(animated: Bool) 
        resultsMovienameArray.removeAll(keepCapacity: false)
        resultsImageFiles.removeAll(keepCapacity: false)
        var query = PFQuery(className: "Videos")
        var objects = query.findObjects()
        for object in objects! 

            self.resultsMovienameArray.append(object["name"] as! String)
            self.resultsImageFiles.append(object["picture"] as! PFFile)





            self.tableView.reloadData()


        


    


    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return resultsMovienameArray.count
    

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCellWithIdentifier("movieCell", forIndexPath: indexPath) as! MovieViewCell

        // Configure the cell...

        cell.movieNameLblTxt.text = self.resultsMovienameArray[indexPath.row]
        resultsImageFiles[indexPath.row].getDataInBackgroundWithBlock 
            (imageData: NSData?, error:NSError?) -> Void in

            if error == nil 

                let image = UIImage(data: imageData!)
                cell.movieImg.image = image

            
        


        return cell

    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
        var cell = tableView.cellForRowAtIndexPath(indexPath) as! MovieViewCell
        self.performSegueWithIdentifier("viewMe", sender: self)
    

     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    var detailScene = segue.destinationViewController as! MovieDetailedViewController
    if segue.identifier == "viewMe" 
        if var indexPath = self.tableView.indexPathForSelectedRow() 
         var object:PFObject = (self.resultsVideoFiles[indexPath.row] as? PFObject)!
         var file:PFFile = object.objectForKey("video") as! PFFile
        (segue.destinationViewController as! MovieDetailedViewController)
    

    









然后这是我的 SecondViewController 的代码

import UIKit
import MediaPlayer
import Parse
import ParseUI

class MovieDetailedViewController: UIViewController 
    var moviePlayer:MPMoviePlayerController!
    var detailItem: AnyObject?
    var videoObject: PFObject!

    override func viewDidLoad() 


        // Do any additional setup after loading the view.
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    

    override func viewDidAppear(animated: Bool) 
        var query = PFQuery(className: "Videos")
        query.findObjectsInBackgroundWithBlock  (objects: [AnyObject]?, error: NSError?) -> Void in

            if error == nil 
                for object in objects! 
                    var theVideo:PFFile = object.objectForKey("video") as! PFFile
                    var url:NSURL = NSURL(string: theVideo.url!)!
                    theVideo.getDataInBackgroundWithBlock( (data:NSData?, error: NSError?) -> Void in
                        if error == nil 
                            self.moviePlayer = MPMoviePlayerController(contentURL: url)
                            self.moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)

                            self.view.addSubview(self.moviePlayer.view)
                            self.moviePlayer.fullscreen = true

                            self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
                            self.moviePlayer.prepareToPlay()
                            self.moviePlayer.play()



                        

                    )

                
            

        
    



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    
    */


非常感谢您的帮助。

【问题讨论】:

【参考方案1】:

我认为它很晚才回答,但你应该这样做在 viewController 中创建一个 PFObject 并使用 indexpath.row 在 didSelectRowAtIndex 方法中这样传递它

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 

    let destinationVC: ViewController = self.storyboard!.instantiateViewControllerWithIdentifier("eVC") as! ViewController

    if let indexPath = self.tableView.indexPathForSelectedRow 
        let row = Int(indexPath.row)
        destinationVC.currentObject = objects?[row] as? PFObject

    

  navigationController?.pushViewController(destinationVC, animated: true)  


我认为这会解决你的问题。

【讨论】:

以上是关于如何将 PFFile 从一个 ViewController 传输到另一个 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

从 Parse 将 PFFile 转换为 NSData

如何将 PFFile 数组转换为 UIImage 数组?

如何限制 PFFile 的后台下载 - Parse Framework

从原始数据创建 AVPlayer 或以不同格式将视频保存到 PFFile

从 PFFile 到 PFFile 的条件转换总是成功

无法将 PFFile 类型的值转换为 NSURL