gpt4 book ai didi

ios - 如何在使用 swift 单击按钮时解析 youtubeURL 以从 parse.com 加载视频

转载 作者:行者123 更新时间:2023-11-30 14:10:08 25 4
gpt4 key购买 nike

我正在尝试在我的 swift 项目中找到从解析数据库加载视频的最佳方法。我的数据库中有一个名为 ProductVideo 的列,其中包含视频的 url。我的项目在表格 View 中有一个产品列表,当推送其中一个单元格时,它会将您带到一个新的 View Controller ,其中显示有关的信息该项目中有一个名为视频的按钮,我想在单击时加载视频。这是我用于打开对象并显示信息的代码。对此提供一些帮助将不胜感激!

@IBOutlet weak var videoView: UIView!
@IBOutlet weak var videoStatus: UILabel!
@IBOutlet weak var productVideo: UIButton!
@IBOutlet weak var productCategory: UILabel!
@IBOutlet weak var ProductImage: PFImageView!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var productDescription: UITextView!



override func viewDidLoad() {
super.viewDidLoad()


// Unwrap the current object object
if let object = currentObject {
productName.text = object["ProductName"] as? String
productDescription.text = object["ProductDescription"] as! String
productCategory.text = object["ProductCategory"] as? String
videoStatus.text = object["VideoStatus"] as? String



var initialThumbnail = UIImage(named: "question")
ProductImage.image? = initialThumbnail!
if let thumbnail = object["ProductImage"] as? PFFile {
ProductImage.file = thumbnail
ProductImage.loadInBackground()


if self.videoStatus.text == "None"
{
self.productVideo.hidden = true
self.videoView.hidden = true
}


}



}





}

vvv这是在Tableview Controller 中..^^^^上面的代码来自productViewController

//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell!
if cell == nil {
cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}

// Extract values from the PFObject to display in the table cell
if let ProductName = object?["ProductName"] as? String {
cell.ProductName.text = ProductName
}


// Display product image
var initialThumbnail = UIImage(named: "question")
cell.ProductImage.image? = initialThumbnail!
if let thumbnail = object?["ProductImage"] as? PFFile {
cell.ProductImage.file = thumbnail
cell.ProductImage.loadInBackground()
}



return cell
}

将其添加到 View Controller

@IBAction func productVideo2(sender: AnyObject) {


let videoWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

let url = object["ProductVideo"] as! String // GET THIS FROM YOUR PARSE OBJECT

let urlRequest = NSURLRequest(URL: NSURL(string: url)!)

videoWebView.loadRequest(urlRequest)
self.view.addSubview(videoWebView)


}

最佳答案

根据您的代码,将如下完成:

override func viewDidLoad() {
super.viewDidLoad()


// Unwrap the current object object
if let object = currentObject {
productName.text = object["ProductName"] as? String
productDescription.text = object["ProductDescription"] as! String
productCategory.text = object["ProductCategory"] as? String
videoStatus.text = object["VideoStatus"] as? String



var initialThumbnail = UIImage(named: "question")
ProductImage.image? = initialThumbnail!
if let thumbnail = object["ProductImage"] as? PFFile {
ProductImage.file = thumbnail
ProductImage.loadInBackground()


if self.videoStatus.text == "None"
{
self.productVideo.hidden = true
self.videoView.hidden = true
} else {


let videoWebView = UIWebView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))

let url = object["ProductVideo"] as! String // GET THIS FROM YOUR PARSE OBJECT

let urlRequest = NSURLRequest(URL: NSURL(string: url)!)

videoWebView.loadRequest(urlRequest)
self.view.addSubview(videoWebView)

}


}



}





}

更新

要按照您的意愿使用它,您需要创建一个自定义 UIButton 子类,其中包含一个属性来保存 cellForRowAtIndexPath 方法中存在的视频 Url

Storyboard

请注意右侧类检查器中的自定义 UITableViewCell 和自定义 UIButton 类分配。

enter image description here enter image description here

UIButton 子类示例

import UIKit

class MyCustomButton: UIButton {

var videoUrl = ""

}

UITableViewCell 子类示例

import UIKit

class MyCustomTableViewCell: UITableViewCell {

@IBOutlet weak var cellBttn: MyCustomButton!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

View Controller 示例

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//MARK: - ivars
var myData = ["One","Two","Three","Four","Five"] // Really just where your video URL is in your code

//MARK: - Overrides
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

//MARK: - UITableViewMethods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return myData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCustomTableViewCell

cell.cellBttn.videoUrl = myData[indexPath.row]

cell.cellBttn.addTarget(self, action: Selector("cellBttnTouched:"), forControlEvents: UIControlEvents.TouchUpInside)

return cell

}

//MARK: - Actions
func cellBttnTouched(sender: MyCustomButton) {

// Spawn your UIWebView here, you can get current video URL from the sender

let thisCellsVideoUrl = sender.videoUrl
println(thisCellsVideoUrl)


}

}

此示例在点击按钮时产生以下输出: enter image description here

关于ios - 如何在使用 swift 单击按钮时解析 youtubeURL 以从 parse.com 加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31864216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com