晚上好。我在将 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.
}
*/
}
我将非常感谢您的帮助。