迅速:UIlabel文本属性已更改,但UI中的显示值未更改

问题描述

用户单击集合视图项时,我想更改UILabelView文本属性

// This is another viewController not the one containing the label
// Handle collectionViewItem selection
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
    print("INSIDE TableViewCell2.collectionView 3")
    TabsBarController.sharedInstance.testTitle = "UILabelText"
    print("didSelectItem\(indexPath)")
}

设置完成后,我尝试在此处进行更新:

  class TabsBarController: UIViewController {
    static let sharedInstance = TabsBarController()
    var movieTitle:  UILabel? = UILabel(frame: CGRect(x: 0,y: 0,width: 300.00,height: 30.00));
    var testTitle: String? = nil {
        didSet {
            print("testTitle.didSet: ",testTitle!) // logs the correct text
            movieTitle?.text = testTitle
            print(" movieTitle?.text : ",movieTitle?.text ) // logs the correct text
        }
    }
}

这里的问题是,即使movieTitle?.text用户界面中,movieTitle UILabel也不会改变。 我已经阅读了许多类似问题的答案,并且所有这些都指向使用主线程,因此我添加了以下内容

 class TabsBarController: UIViewController {
        static let sharedInstance = TabsBarController()
        var movieTitle:  UILabel? = UILabel(frame: CGRect(x: 0,height: 30.00));
        var testTitle: String? = nil {
            didSet {
                // I added this but still nothing changed.
                dispatchQueue.main.async {
                    // Run UI Updates
                    print("testTitle.didSet: ",testTitle!) // logs the correct text
                    movieTitle?.text = testTitle
                    print(" movieTitle?.text : ",movieTitle?.text ) // logs the correct text
                }
                
            }
        }
    }

但是,UI仍然没有更新。知道为什么会这样以及如何解决吗?

注意::这是层次结构:
层次结构就像这样:TabsBarViewController-> MoviesViewController-> UITableView-> UitableViewCell-> CollectionView

解决方法

基于项目层次结构,我必须遵循说明here,以便能够从UITabsBarController内部访问testTitle tableViewCell属性。 请注意,我必须进行以下转换:

 // Handle collectionViewItem selection
    func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
        print("INSIDE TableViewCell2.collectionView 3")
   
          if let vc2 =  self.viewController?.parent as? TabsBarController {
             vc2.testTitle = "THIS WILL DEFINITELY ABSOLUTELY WORK I DONT CARE"
        }
        print("didSelectItem\(indexPath)")
    }