Я создаю приложение для обучения танцам. Мой первый View Controller - это коллекционное представление с изображениями в ячейках. При нажатии на ячейку он открывает данные для каждой ячейки. После этого я обновляю все мои представления при выборе ячеек во втором View Controller, и мои метки обновляются. Но также у меня есть кнопка видео на моем втором контроллере вида. Проблема в том, что я хочу при выборе ячеек обновить видео кнопки выбранного видео.
Я гуглил, но не нашел ничего, что могло бы мне помочь.
Это мой контроллер второго вида.
import UIKit
import AVKit
let detailCollectionViewReuseIdentifier = "detailCollectionViewCell"
class DetailViewController: UIViewController {
@IBOutlet weak var detailCollectionView: UICollectionView!
@IBOutlet weak var navItem: UINavigationItem!
@IBOutlet weak var danceLearnName: UILabel!
@IBOutlet weak var courseButton: UIButton!
@IBOutlet weak var detailDanceDescription: UITextView!
@IBOutlet weak var mediaDanceName: UIButton!
var detailDance: Dance? {
didSet {
configureView()
}
}
func configureView() {
if let detailDance = detailDance {
if let navItem = navItem, let danceLearnName = danceLearnName, let courseButton = courseButton, let detailDanceDescription = detailDanceDescription, let mediaDanceName = mediaDanceName {
detailDanceDescription.text = detailDance.danceCourseDescription[0]
danceLearnName.text = detailDance.danceCourseName[0]
mediaDanceName.setTitle("Մեդիա", for: .normal)
courseButton.layer.masksToBounds = true
courseButton.layer.cornerRadius = 15
navItem.title = detailDance.danceName + " Ուսուցում"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showVideos" {
if let destination = segue.destination as? DanceMediaViewController {
destination.detailDance = detailDance
}
}
}
func playMedia(_ path: String) {
guard let videoPath = Bundle.main.path(forResource: path, ofType: "mov") else {
debugPrint("\(String(describing: path)) not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: videoPath))
let playerController = AVPlayerViewController()
playerController.player = player
present(playerController, animated: true) {
player.play()
}
}
@IBAction func mediaStepClicked() {
let indexPath = (detailDance?.danceMediaBySteps[0])!
playMedia(indexPath)
}
}
extension DetailViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (detailDance?.detailDanceImages.count)!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: detailCollectionViewReuseIdentifier, for: indexPath) as! DetailCollectionViewCell
cell.detailDanceImages.image = UIImage(named: (detailDance?.detailDanceImages[indexPath.item])!)
cell.detailDanceImages.layer.cornerRadius = 8
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: false)
collectionView.delaysContentTouches = false
detailDanceDescription.text = detailDance?.danceCourseDescription[indexPath.item]
danceLearnName.text = detailDance?.danceCourseName[indexPath.item]
}
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! DetailCollectionViewCell
cell.contentView.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
}
func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! DetailCollectionViewCell
cell.contentView.backgroundColor = UIColor.clear
}
}
Я хочу получить совет о том, как интегрировать правильную функцию, когда didSelect на моей ячейке, и обновить видео с нажатой кнопкой.