在多个视图控制器上使用委托和协议快速更改主题 [英] swift change theme using delegate and protocol on many view controller
问题描述
我正在尝试第一次使用委托和协议。
我想在许多视图控制器中更改主题。
然后在任何具有更改主题协议的控制器上
现在我使用该控制器时,我希望主题是新的但很旧。 p>
我不会从主题控制器转到它们已更改的地方
我的代码
协议ThemeDelegate:类{
func changeTheme(theme:UIColor)
}
class FirstController:UICollectionViewController,UICollectionViewDelegateFlowLayout,ThemeDelegate {
var newTheme:UIColor = .red
func changeTheme(theme:UIColor){
newTheme = theme
}
覆盖func viewDidLoad(){
super.viewDidLoad()
view.backgroundColor = newTheme
}
}
ThemeController {
较弱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)为! ThemeCell
cell.themeImageView.image = UIImage(命名为主题单元格图像)
返回单元格
}
}
我将尝试使其变得简单:-
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
}
}
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
这篇关于在多个视图控制器上使用委托和协议快速更改主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!