[英]Change value when view is dismissed
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. 我有一个NowPlayingVC,作为MainVC(集合视图)的子级,我想在关闭第三个viewcontroller(SingleSoundVC)时更改NowPlayingVC内部的textValue。 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
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 单音VC
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 当我选择添加项目NowPlaying作为MainVC的代表时,我还必须添加一点
MainVC - CollectionView 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 MainVC-CollectionView
you should create a property for NowPlayingVC to reference to the same instance: var nowPlayingVC = NowPlayingVC() 您应该为NowPlayingVC创建一个属性以引用同一实例: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 NowPlayingVC
adopt it to SendDataToAudioPlayerContainer
protocol : add 将其用于SendDataToAudioPlayerContainer
协议:添加
func receiveData(data:Sound){
self.audioNameLabel.text = data.name
**// update constraints here**
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.