【问题标题】:Swift - Not able to hide or unhide UIView considering I can see itSwift - 考虑到我可以看到它,无法隐藏或取消隐藏 UIView
【发布时间】:2021-08-13 18:30:09
【问题描述】:

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

.
├── _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,找到它的播客链接,然后将其传递给 View Controller。

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: {$0.itemId == itemAuthor?.itemId}) {
            podcastLink = allItems?.itemListv2[itemOffset].podcastsound
         }
        
         let url = podcastLink ?? " "
         delegate?.podcastButtonClicked(podcastUrl: URL)
     }
}

这是包含 Podcast Player UIView、AVPlayer Player、PodcastViewCells 的 Collection View 等的 View Controller 代码。我还在代码中添加了可以和不能隐藏/取消隐藏 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)
    }
}

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

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    问题来了

    DetailViewController().setupPodcastPlayer(link: url)
    

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

    编辑:单元格内

    weak var delegate: DetailViewController?
    

    并将其设置为cellForRowAt

    cell.delegate = self
    

    【讨论】:

    • 感谢您的评论!那我应该怎么做是通过代理传递 URL,然后更改视图的可见性,对吗?
    • 我已经用委托更改更新了代码。这次我的播放器完全停止播放,我认为是因为 setupPodcastPlayer 根本没有运行。
    • 添加断点并按预期跟踪您的代码顺便说一句,这是您的问题现在得到解答的另一个问题
    • cell.delegate = self 吗?在cellForRowAt 内?
    • 是的,我注意到了,并且正在修复它。那是通过的问题。委托方法完美运行,感谢您的努力!
    猜你喜欢
    • 2021-09-27
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多