1

I have a super simple app with root view controller and modal view controller.

At first view i have only one UIButton - it animate transition to modal controller. At modal view controller I have 4 sliders (R,G,B,Alpha), they are changing modal view background color and one UIBUtton to exit from modal view. By clicking this button background color of my root controller needs to be changed to the same color as in modal controller. How can I do this and make my modal controller act to achieve this?

Also I need to save this preferences to NSUserDefaults. So What is the most correct way to do this?

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
Olex
  • 1,656
  • 3
  • 20
  • 38

2 Answers2

2

Hi Here is a quick reference for some of the things you can do with NSUserDefaults. You can use this for saving the data in NSUserDefault

Saving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:42 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:3.1415 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];

// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];

Retrieving

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"floatKey"];

And you can get the RGB Values using utilities like CGFloat red = [myColor red]; and CGFloat Green= [myColor Green]; like wise and use it to save them.

Or you can check Get RGB value from UIColor presets for the same

Community
  • 1
  • 1
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
  • Ok, but how can i save color object? UIColor is not supported by NSUserDefault, I also tried to save an NSArray with UICOLor object, but project crashed in this place – Olex Jul 17 '12 at 12:43
  • 1
    provided way to get colors and save them CGFloat red = [myColor red]; where myColor will be UIColor and then SAVE the RGB individually in NSUserDefault... – Jigar Pandya Jul 17 '12 at 12:49
1

Read up on the delegate pattern to do your first thing; I've posted a small tutorial I wrote below. The other answer looks like it should be sufficient for NSUserDefaults

Delegates

//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"segueName"])
    {
        childController *foo = segue.destinationViewController;
        foo.delegate = self;
    }

}

//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
    //An example of what you could do if your data was an NSDate
    buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}

//In parent .h file:
//import child header
#import "ChildName.h"

//indicate conformity with protocol
@interface ParentName : UIViewController <ChildNameDelegate>

//In child .h file
//declare protocol
@protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
@end

//declare delegate
@property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;


//In child .m file
//synthesize delegate
@synthesize delegate; 

//use method
- (IBAction)actionName:(id)sender 
{
    [delegate methodName:assignedData];
}

Here's another example Simple Delegate Example?

Community
  • 1
  • 1
Dustin
  • 6,783
  • 4
  • 36
  • 53
  • Can you rewrite this code without using storyboard methods, please? – Olex Jul 17 '12 at 12:46
  • 1
    I'm not sure exactly what you're doing, but everything would be the same except for the `prepareForSegue` method, which would basically just be replaced by whatever code you're using to present the modal view controller. – Dustin Jul 17 '12 at 12:47
  • I am sorry, and one more question: in my case modal controller is parent and root is a child? – Olex Jul 17 '12 at 12:54
  • 1
    No, whatever presents the modal controller should be the parent (it's "giving birth" to the other view). So the method to be executed is in the parent and the call to the method is in the child. – Dustin Jul 17 '12 at 13:46