I'm trying to use delegate and protocol first time.
I want to change the theme across many view controller.
Then on any controller which has protocol to change theme
When I go to this controller now I expect theme to be new but is old.
I do not go from theme controller to where them has changed
My code
protocol ThemeDelegate: class {
func changeTheme(theme: UIColor)
}
class FirstController: UICollectionViewController, UICollectionViewDelegateFlowLayout, ThemeDelegate {
var newTheme: UIColor = .red
func changeTheme(theme: UIColor) {
newTheme = theme
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = newTheme
}
}
ThemeController {
weak var themeDelegate: ThemeDelegate?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let theme = .blue
themeDelegate?.changeTheme(theme: theme)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: themeCellId, for: indexPath) as! ThemeCell
cell.themeImageView.image = UIImage(named: "theme cell image")
return cell
}
}