如何将PFFile从一个ViewController转移到另一个ViewController

时间:2015-07-06 03:51:04

标签: swift parsing ios8 xcode6 pffile

晚上好。我将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 个答案:

答案 0 :(得分:0)

我认为答案已经很晚但你应该在viewController中创建一个PFObject并在didSelectRowAtIndex方法中使用indexpath.row这样传递

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)  

}

我认为这将解决您的问题。