微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Swift - 考虑到我可以看到它,无法隐藏或取消隐藏 UIView

我有一个用于播客播放器应用程序的两文件项目。我的目标是当用户单击播放按钮(加载到 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,找到它的播客链接,然后将其传递给视图控制器。

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 的集合视图等的视图控制器代码。我还在代码添加了可以和不能隐藏/取消隐藏 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)
    }
}

感谢您的建议,祝您有美好的一天!

解决方法

问题来了

UIButton

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

编辑:在单元格内

DetailViewController()

并将其设置在 weak var delegate: DetailViewController?

cellForRowAt

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。