[英]Change all the app color scheme
So, I have a HomeViewController (picture 1) with two buttons, one white and one blue. 因此,我有一个带有两个按钮的HomeViewController(图1),一个为白色,一个为蓝色。 On the right bottom corner you can see a button which modally presents a SettingsViewController (picture 2), on this view controller there are 4 buttons so the user can choose which color scheme do they prefer. 在右下角,您可以看到一个按钮,该按钮以模态呈现一个SettingsViewController(图2),在该视图控制器上,有4个按钮,因此用户可以选择他们喜欢的配色方案。 Imagine the user press the first one (red) then, when dismissing the view controller the color scheme of HomeViewController should look like picture 3. 想象用户按下第一个(红色),然后在关闭视图控制器时,HomeViewController的配色方案应如图3所示。
Any ideas on how to do this on a efficiently/simple way?. 关于如何有效/简单地执行此操作的任何想法?
There are two good ways you could do this: 1) Delegation, and 2) viewWillAppear:
. 您可以通过两种方法进行此操作:1)委派,以及2) viewWillAppear:
For delegation , you'll need to define a protocol. 对于委派 ,您需要定义一个协议。 Your HomeViewController
will be a delegate
for this protocol, and your SettingsViewController
will call it. 您的HomeViewController
将是此协议的delegate
,而您的SettingsViewController
将调用它。
//SettingsViewController.h
@protocol SettingsDelegate <NSObject>
@required
-(void)colorChanged:(UIColor *)color;
@end
@interface SettingsViewController : UIViewController
@property (nonatomic, weak) id<SettingsDelegate> delegate;
@end
Somewhere when the settings view controller is set up, make sure to set self.delegate
equal to a reference to HomeViewController
. 设置设置视图控制器时,请确保将self.delegate
设置为等于对HomeViewController
的引用。 This is a must. 这是必须的。
Then, when your user changes the color, call: 然后,当您的用户更改颜色时,请致电:
[self.delegate colorChanged:whateverColor];
Your delegate must obviously observe this method, and change the color appropriately: 您的代表显然必须遵守此方法,并适当更改颜色:
-(void)colorChanged:(UIColor *)color {
[myButton setBackgroundColor:color];
}
For viewWillAppear:
, just save the color somewhere and set the color of your button in your view controller's method for this. 对于viewWillAppear:
只需将颜色保存在某个位置,然后在视图控制器的方法中设置按钮的颜色即可。 viewWillAppear:
will get called when your settings view is about to disappear and show the home view controller: viewWillAppear:
当您的设置视图即将消失并显示主视图控制器时,将被调用:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myButton setBackgroundColor:mySavedColor];
}
In HomeViewController
On SettigButton Click
{
Pass the HomeViewController delegate object SettingsViewController
Present your color Picker SettingsViewController
}
In SettingsViewController
Define protocol name SettingsViewControllerDelegate
{
-(void)selectedColor:(UIColor*)color;
}
return the selected color On dismissViewController
if(delegate)
{
[delegate selectedColor:color];
}
Again In HomeViewController
-(void)selectedColor:(UIColor*)color
{
view.backgroundColor=color;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.