ios - 关闭 View 时更改值

标签 ios swift uicollectionview delegates

我有一个 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/

相关文章:

ios - 为什么 Firebase 数据库 queryEqualToValue 不起作用?

ios - 在 Quartz 中创建 PDF 时计算页数的最佳方法?

ios - 禁用触摸检测 UIScrollView

ios - UICollectionView 不响应 UISearchBar

ios - 使用 collectionView 制作动画水平指示器

键盘扩展中 advanceToNextInputMode 后的 iOS 内存泄漏

ios - avplayer 在模拟器上而不是在设备上播放视频?

ios - 迦太基更新 - 任务失败,退出代码 65

json - 来自 `[String: Any]` 字典的 Vapor JSON

ios - 为什么我的 Collectionview cell.image 不见了并且乱七八糟?