1

So, I have a HomeViewController (picture 1) with two buttons, one white and one blue. 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. Imagine the user press the first one (red) then, when dismissing the view controller the color scheme of HomeViewController should look like picture 3.

enter image description here

Any ideas on how to do this on a efficiently/simple way?.

iDec
  • 707
  • 2
  • 8
  • 16

2 Answers2

1

There are two good ways you could do this: 1) Delegation, and 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.

//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. 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: will get called when your settings view is about to disappear and show the home view controller:

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    [myButton setBackgroundColor:mySavedColor];
}
rebello95
  • 8,486
  • 5
  • 44
  • 65
  • I did it using the protocol although I'll have to read a little bit more about delegates and protocols because even though it works I don't what is exactly happening. At first I tried using the viewWillAppear method but how would you set mySavedColor(in HomeViewController) variable from SettingsViewController? – iDec Feb 08 '15 at 17:21
  • @iDeC yes, definitely read more about it and learn how it works. You'd have to save `mySavedColor` somewhere (see [this question](http://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults)) and then retrieve it in `viewWillAppear:`. – rebello95 Feb 08 '15 at 17:31
0
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;

}
Shashi3456643
  • 2,021
  • 17
  • 21