Swift - 无法隐藏或取消隐藏 UIView,因为我可以看到它

Swift - Not able to hide or unhide UIView considering I can see it

我有一个播客播放器应用程序的两个文件项目。我的目标是当用户单击播放按钮(加载到 nib 单元格中)时,将显示 PodcastPlayerView(添加到视图控制器中的 StoryBoard)play/pause。

.
├── _DetailViewController.swift
│   ├── AVPlayer
│   ├── PodcastPlayerView  (full-screen player view)  
│   └── setupPodcastPlayer (setting player's URL & hide/unhide PodcastPlayerView)
│
├── _PodcastViewCell.swift (File Owner of the nib)
│   ├── url (Podcast URL of the cell)
└── └── @IBAction playPodcast (the play button in a cell)

这是 PodcastViewCell.swift 中最多的代码 运行。获取单元格的 itemId,找到其播客 link,然后将其传递给视图控制器。

protocol PodcastViewCellDelegate {
    func podcastButtonClicked(podcastUrl: String)
}

class PodcastViewCell: UICollectionViewCell {
    weak var delegate: PodcastViewCellDelegate?

    @IBAction func playPodcast(_ sender: Any) {
         if let itemOffset = allItems?.itemListv2.firstIndex(where: {[=12=].itemId == itemAuthor?.itemId}) {
            podcastLink = allItems?.itemListv2[itemOffset].podcastsound
         }
        
         let url = podcastLink ?? " "
         delegate?.podcastButtonClicked(podcastUrl: URL)
     }
}

这是包含 Podcast Player UIView、AVPlayer Player、PodcastViewCells 的 Collection View 等的视图控制器代码。我还在代码中添加了可以和不能 hide/unhide PodcastPlayerView 的行。当我 运行 在视图未隐藏的情况下应用程序时,视图是可见的并且位于应有的位置。但是我仍然无法在我提到它的行中取消隐藏它。这就像视图加载并被销毁...

class DetailViewController: BaseViewController {
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var podcastPlayerView: UIView!

    var podcastLink: String = ""
    static var itemId = "0"

    var player: AVPlayer?
    var playerItem: AVPlayerItem?
    var timeObserverToken: Any?
    var played = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.podcastPlayerView.isHidden = false "<-------- Unhiding/hiding process successful in this step"
    }
    override func viewWillDisappear(_ animated: Bool) {
        showPodcastButton = false
        player?.pause()
        player?.replaceCurrentItem(with: nil)
        player = nil
    }
    func setupPodcastPlayer(link: String) {
        player?.pause()
        player?.replaceCurrentItem(with: nil)
        player = nil
        if !played {
            if link != "" {
                playerItem = AVPlayerItem( url:NSURL( string:link )! as URL )
                player = AVPlayer(playerItem:playerItem)

                player!.rate = 1.0;
                player!.play()

                played = true
                podcastPlay()
            } else {
                "link empty"
            }
        } else {
            player?.replaceCurrentItem(with: nil)
            played = false
        }
    }
    func podcastPlay() {
         self.podcastPlayerView.isHidden = false "<---- If I try to unhide here, app crashes."
    "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
    }
}

extension DetailViewController: PodcastViewCellDelegate {
    func podcastButtonClicked(podcastUrl: String) {
        podcastPlayerView.isHidden = false
        setupPodcastPlayer(link: podcastUrl)
    }
}

感谢您的任何建议,祝您有愉快的一天!

问题出在这里

DetailViewController().setupPodcastPlayer(link: url)

这个 DetailViewController() 是一个新实例,而不是当前实例,挂钩实际显示的实例,并根据需要通过委托或通知更改其属性

编辑: 在单元格内

weak var delegate: DetailViewController?

并设置在cellForRowAt

cell.delegate = self