【问题标题】:Segue to different controllers from one cell (a detail view and a avcontroller)从一个单元格转到不同的控制器(详细视图和 avcontroller)
【发布时间】:2019-10-19 14:16:11
【问题描述】:

如果有视频,点击单元格时,我只想显示视频(在 avPlayerViewController 中),如果没有视频,它会转到详细视图w

我试过这个:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    let post = postArray[indexPath.item]
    let controller = PostDetailViewController.fromStoryboard(post: post)
    self.navigationController?.pushViewController(controller, animated: true)


    guard let videoURL = URL(string: post.videoLink)  else {
        return
            }

    let avPlayer = AVPlayer(url: videoURL)
    let avController = AVPlayerViewController()
    avController.player = avPlayer

    present(avController, animated: true) {
        avPlayer.play()
    }

}

如果有视频,它会播放视频(来自 Firebase),但它也会打开详细视图。所以我希望单元格只打开视频或只打开详细视图

【问题讨论】:

    标签: ios swift avfoundation avkit


    【解决方案1】:

    构造它,如果没有post.videoLink,则推送PostDetailViewController,否则将播放视频:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
        guard let videoURL = URL(string: post.videoLink) else {
            //If there is no URL, then this else condition will be called. Otherwise, the code below the guard statement will be called, and the video player will appear.
            let post = postArray[indexPath.item]
            let controller = PostDetailViewController.fromStoryboard(post: post)
            self.navigationController?.pushViewController(controller, animated: true)
            return
        }
    
        let avPlayer = AVPlayer(url: videoURL)
        let avController = AVPlayerViewController()
        avController.player = avPlayer
    
        present(avController, animated: true) {
            avPlayer.play()
        }
    
    }
    

    【讨论】:

    • 我尝试了类似if post.videoLink == "" 的方法,但没有成功。我该怎么做?
    • 好吧,当一个帖子没有与之关联的视频时,post.videoLink 等于 ”” 还是等于 nil
    • 当我让它等于 nil 时,我得到'nil' cannot be assigned to type 'String'。我对 swift 有点陌生,所以我不知道该怎么做。
    • 您能在没有视频的帖子中查看post.videoLink 的值吗?我想它等于””。它不能等于nil
    • 感谢您的帮助,当我将pushViewController(controller, animated: true) 放入if post.videoLink == "" 时,它起作用了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多