从另一个类的函数设置UIbutton图像

时间:2019-06-20 01:33:11

标签: ios swift uicollectionview

所以我有一个歌曲列表,当我单击moreButton时,将显示SongDetailCell并在所选歌曲上调用setPlayPauseOnAppeaering()函数。

此函数将根据if语句设置播放或暂停图标。由于某些原因,即使正在打印正确的控制台日志,也没有暂停图标。例如,当“播放Spotify样本” 时,该按钮没有设置为暂停,即使它确实应该暂停。有人知道我在这里做错什么吗?

class SongList: UICollectionViewCell    {

    @objc func moreButtonTap(gesture: UITapGestureRecognizer)  {

        var indexPath = getIndexPathFromRecognizer(gesture: gesture)

        let songDetailView = SongDetailCell()
        let song = self.songsArray[indexPath.item]
        songDetailView.song = song
        songDetailView.setPlayPauseOnAppearing()
    }
}

class SongDetailCell: UICollectionViewCell {
    var song: Song! 

    let playPauseButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "play-button-large"), for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.tintColor = UIColor.brandBlack()
        button.contentMode = .scaleToFill
        return button
    }()

func setPlayPauseOnAppearing()    {

    func setPlayButton()  {
        playPauseButton.setImage(UIImage(named: "play-button-large")?.withRenderingMode(.alwaysTemplate), for: .normal)
    }
    func setPauseButton()    {
        playPauseButton.setImage(UIImage(named: "pause-button-large")?.withRenderingMode(.alwaysTemplate), for: .normal)
    }

    if SongPlayer.shared.doesPreviewExist(spotifyPreviewUrl: spotifyURL, iTunesPreviewUrl: itunesURL)   {
        if SongPlayer.shared.player.timeControlStatus == .paused    {
            if SongPlayer.shared.previewUrl == spotifyURL {
                setPlayButton()
                print("paused with Spotify sample")
            } else if SongPlayer.shared.previewUrl == itunesURL {
                setPlayButton()
                print("paused with iTunes sample")
            }
        }   else if SongPlayer.shared.player.timeControlStatus == .playing {
            if SongPlayer.shared.previewUrl == spotifyURL {
                setPauseButton()
                print("playing with Spotify sample")
            } else if SongPlayer.shared.previewUrl == itunesURL {
                setPauseButton()
                print("playing with iTunes sample")
            }
        }   else    {
                print("no audio sample")
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

您正在创建一个按钮(playPauseButton)。但是,您的代码中没有一个实际上将按钮置于界面中。因此,您永远不会看到它。您可以很好地更改图标,但是该按钮在内存中的某个位置处于关闭状态。

单元格是平行的。你在说

let songDetailView = SongDetailCell()

但这只是一个新的细胞,它在太空中的某个地方消失了。接口中实际不存在单元。因此,您对它执行的操作没有任何可见的效果(然后只是消失在烟雾中)。