gpt4 book ai didi

ios - 关闭 View 时更改值

转载 作者:行者123 更新时间:2023-11-28 07:22:58 25 4
gpt4 key购买 nike

我有一个 NowPlayingVC,作为我的 MainVC( Collection View )的子级,我想在第三个 View Controller (SingleSoundVC) 被关闭时更改 NowPlayingVC 中的文本值。我通过代码做所有事情,我不明白为什么我的标签在解雇后仍然不可见。如果我努力编码,它们会工作得很好,但永远不会改变。

nowPlayingVC

当第三个 View 被关闭但标签为空时,我可以正确打印,即使我可以在调试 View 层次结构中看到它。

我试过像这样使用协议(protocol)/委托(delegate):

协议(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")}

}

我还必须添加一点,当我选择项目时,我将 NowPlaying 添加为 MainVC 的委托(delegate)

MainVC - Collection View

  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 - Collection View

您应该为 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)

应该定义为:

    protocol SendDataToAudioPlayerContainer {
func receiveData(data:Sound)
}

NowPlayingVC

将其采用到 SendDataToAudioPlayerContainer 协议(protocol)中:添加

func receiveData(data:Sound){
self.audioNameLabel.text = data.name
**// update constraints here**
}
}

关于ios - 关闭 View 时更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com