When a user clicks on a collection view item, I want to change the UILabelView text property:
// 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)")
}
Once it's set, I try to update it here:
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
}
}
}
The problem here is that even though movieTitle?.text
, in the UI, the movieTitle
UILabel
doesn't change. I've read many answers to similar question, and all of them point to using the main thread, so I added this:
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 {
// 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
}
}
}
}
But, still the UI doesn't get updated. Any idea why is this happening and how to solve it?
NOTE: This is the hierarchy :
The hierarchy is like this TabsBarViewController-> MoviesViewController -> UITableView->UitableViewCell->CollectionView
Based on the project hierarchy, I had to follow the instructions here in order to be able to access the UITabsBarController
testTitle
property from inside the tableViewCell
. Just one caveat, I had to do this casting:
// 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)")
}
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.