I have a NowPlayingVC, as a child of my MainVC(a collection view), I'd like to change a textValue inside the NowPlayingVC when a third viewcontroller (SingleSoundVC) get dismissed. I do everything via code, and I cant understand why my labels are still not visible after the dismiss. If I try hard coding they work just fine, but never change.
I can print correctly when the third views gets dismissed but the label is empty, even though I can see it with the debug view hierarchy.
I tried with Protocol/Delegate like this:
Protocol
protocol SendDataToAudioPlayerContainer {
func receiveData(data:Sound){
self.audioNameLabel.text = data.name
}
}
NowPlayingVC
NowPlayingVC: SendDataToAudioPlayerContainer
var audioNameLabel:UILabel = {
var lbl = UILabel()
lbl.numberOfLines = 0
lbl.textAlignment = .left
lbl.sizeToFit()
lbl.textColor = .black
return lbl
}
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupConstraints()
}
func setupViews() {
self.view.backgroundColor = .blue
self.view.addSubview(audioNameLabel)
}
func setupConstraints(){ //setup of constraints with SnapKit}
}
SingleSoundVC
var delegate:SendDataToAudioPlayerContainer?
var singleSound: Sound?
@objc func dismissView(){
if self.delegate != nil {
print("data passed up")
let data = self.singleSound
delegate?.receiveData(data: data!)
self.dismiss(animated: true, completion: nil)
} else {self.dismiss(animated: true, completion: nil)
print("data is not passed")}
}
I also have to add that when I select the item I added NowPlaying as the delegate of MainVC
MainVC - CollectionView
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = MainVC()
let childVC = NowPlayingVC()
vc.delegate = childVC
ApiService.sharedInstance.downloadAudioFile(with: vc.singleSound!.audioId)
vc.modalPresentationStyle = .popover
present(vc, animated: true, completion: nil)
}
MainVC - CollectionView
you should create a property for NowPlayingVC to reference to the same instance: var nowPlayingVC = NowPlayingVC()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = SingleSoundVC()
vc.delegate = nowPlayingVC
ApiService.sharedInstance.downloadAudioFile(with: vc.singleSound!.audioId)
vc.modalPresentationStyle = .popover
present(vc, animated: true, completion: nil)
}
Protocol
should be defined as:
protocol SendDataToAudioPlayerContainer {
func receiveData(data:Sound)
}
NowPlayingVC
adopt it to SendDataToAudioPlayerContainer
protocol : add
func receiveData(data:Sound){
self.audioNameLabel.text = data.name
**// update constraints here**
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.