我试图创建一个能够改变第一个视图控制器的全局色调的设置页面。
继承人我拥有的东西(设置视图控制器&是的颜色还没有):
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"settingsSeque"]) {
ViewController *tintColorChange = [segue destinationViewController];
tintColorChange.view.tintColor = self.view.tintColor;
}
}
- (IBAction)tapRed:(UITapGestureRecognizer *)sender {
self.view.tintColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
}
- (IBAction)tapYellow:(UIButton *)sender {
self.view.tintColor = [UIColor colorWithRed:.5 green:.3 blue:.1 alpha:1];
}
- (IBAction)tapGreen:(UIButton *)sender {
self.view.tintColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];
}
- (IBAction)tapBlue:(UIButton *)sender {
self.view.tintColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
}
- (IBAction)tapOrange:(UIButton *)sender {
self.view.tintColor = [UIColor colorWithRed:.75 green:.5 blue:0 alpha:1];
}
对于第一个视图控制器:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.tintColor = tintColorChange;
我做错了什么?
答案 0 :(得分:1)
在SettingsViewController中创建一个委托引用:
@interface SettingsViewController ()
@property (nonatomic) FirstViewController *delegateReference;
@end
将FirstViewController实例设置为delegateReference:
settingsViewController.delegateReference = firstViewController;
在FirstViewController中创建一个方法:
- (void)setBackgroundColor:(UIColor *)color {
self.view.backgroundColor = color;
}
现在在您的SettingsViewController按钮点击事件回调:
- (IBAction)tapGreen:(UIButton *)sender {
if (self.delegateReference) {
[self.delegateReference setBackgroundColor:[UIColor greenColor]];
}
}
同样正如Afonso Tsukamoto指出的那样,将delegateReference放在单例类中是个好主意。