快速,从另一个类更改xib中的UI

时间:2019-09-11 15:27:32

标签: swift xcode image button

我正在创建一个播放背景声音的应用程序。单击声音按钮后,我想在播放/暂停按钮中更改播放按钮的图像(请参见下图)。

播放/暂停按钮工具栏位于单独的xib文件(PlayBar)中,我不知道如何从其他类(SoundPresenter)中更改按钮图像。

这是我要更改按钮图像的课程:

class PlayBar: UIView {

let kCONTENT_XIB_NAME = "PlayBarView"
@IBOutlet weak var playBtnOutlet: UIButton!
@IBOutlet weak var favoriteBtnOutlet: UIButton!
@IBOutlet weak var timerBtnOutlet: UIButton!
@IBOutlet var playBarView: UIView!
private let soundsCurrentlyPlaying = SoundsCurrentlyPlaying()
static var currentWindow = UIViewController()

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

func commonInit() {
    Bundle.main.loadNibNamed(kCONTENT_XIB_NAME, owner: self, options: nil)
    playBarView.fixInView(self)

    playBtnOutlet.addTarget(self, action: #selector(self.playBtnAction(sender:)), for: .touchUpInside)
}

@objc func playBtnAction(sender: UIButton){
    print("In PlayBtnAction")
    playBtnOutlet.setImage(UIImage(named: "pause"), for: .normal)
}

这是我要从中应用更改的班级:

    protocol SoundDelegate : NSObjectProtocol{
    func soundBtnSelected(senderOutlet : UIImageView, soundName : String)
    func soundBtnUnselected(senderOutlet : UIImageView, soundName : String)
    func changeSliderImage(sender : UIButton, senderOutlet: UIImageView, soundName : String)
    func removeSliderImage(senderOutlet : UIButton)
}

    class SoundPresenter {
        weak private var soundDelegate : SoundDelegate?
        private var soundsCurrentlyPlaying = SoundsCurrentlyPlaying()
        private var playBar = PlayBar()
        private var audioNames = AudioFiles()
        private var firstSliderOutlet : UISlider?
        private var secondSliderOutlet : UISlider?
        private var thirdSliderOutlet : UISlider?


    func setSoundViewDelegate(soundDelegate : SoundDelegate){
        self.soundDelegate = soundDelegate
    }

    func soundButtonClicked(senderOutlet : UIImageView, sender: UIButton){
        sender.isSelected = !sender.isSelected
        let soundName = audioNames.provideAudioName(senderTag: sender.tag)

        if sender.isSelected{

            self.soundDelegate?.changeSliderImage(sender : sender, senderOutlet : senderOutlet, soundName : soundName)
            self.soundDelegate?.soundBtnSelected(senderOutlet : senderOutlet, soundName : soundName)
            soundsCurrentlyPlaying.saveCurrentSound(soundName: soundName)


            if !playBar.playBtnOutlet.isSelected{
                print("Change to selected")
                //Programmatically sends an action to button
                playBar.playBtnOutlet.sendActions(for: .touchUpInside)
            }


        } else {
            self.soundDelegate?.removeSliderImage(senderOutlet : sender)
            self.soundDelegate?.soundBtnUnselected(senderOutlet: senderOutlet, soundName: soundName)
        }
    }

这会触发在playBtnAction内部打印“ In PlayBtnAction”,但不会更改按钮的外观。

但是,如果我直接单击应用程序中的播放按钮,它将按预期更改图像。

非常感谢您的帮助!

0 个答案:

没有答案