且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在多个视图控制器上使用委托和协议快速更改主题

更新时间:2022-10-16 18:04:00

我将尝试使其变得简单:-



1 ),首先声明一个协议:-

  protocol ThemeDelegate {
func changeTheme(theme:String,fontColor:UIColor, alpha:CGFloat)
}

2)在ThemeController中具有该协议的变量:

  open var themeDelegate:ThemeDelegate? = nil 

3)通过 themeDelegate $ c $调用委托函数c> :-(直到这一步为止,您都已完成此操作)

  func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath: IndexPath){
themeDelegate?.changeTheme(theme:theme,fontColor:fontColor,alpha:alpha)
}

4)您需要符合您的 AnyController 作为委托,例如 yourThemeControllerInstance.delegate = self 并且您也已经完成了。



它不起作用,因为您已声明 ThemeController 的新实例并符合 AnyController 作为该新实例的委托,该新实例应该具有相同的旧主题:

  override func viewDidLoad(){
let themeController = ThemeController()//这是您创建的新实例,因此将使用旧主题进行初始化。您已使您的类成为该实例的委托
themeController.themeDelegate = self
}

为了使您的代表按预期工作,您需要使用同一实例的 ThemeController

>

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
    }
}

this is order

image2

I'll try to make it simple:-

1) First, declare a protocol:-

protocol ThemeDelegate{
    func changeTheme(theme: String, fontColor: UIColor, alpha: CGFloat)
}

2) Have a variable of that protocol in your ThemeController:

open var themeDelegate: ThemeDelegate? = nil

3) Call the delegate functions through themeDelegate:- (you've done this right till this step)

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    themeDelegate?.changeTheme(theme: theme, fontColor: fontColor, alpha: alpha)
}

4) You need to conform your AnyController as the delegate, like yourThemeControllerInstance.delegate = self and you've done that as well.

It doesn't work because you've declared a new instance of ThemeController and have conformed AnyController to be the delegate of that new instance which supposedly has the same old theme:

override func viewDidLoad() {
    let themeController = ThemeController() // This is a new instance which you have created, so gets initialised with old theme. You have made your class to be the delegate of this instance
    themeController.themeDelegate = self
}

In order for your delegate to work as expected, you need the same instance of ThemeController where you change your theme