Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:39:22+00:00 2026-06-09T20:39:22+00:00

I have a UISwitch, witch among other things, disables a UITextfield. When using NSUserDefaults,

  • 0

I have a UISwitch, witch among other things, disables a UITextfield.
When using NSUserDefaults, I manage the switch to remember its current state (on/off)

NSString *const MyPrefKey =@"MyPrefKey";


 [[NSUserDefaults standardUserDefaults]
     setBool:[sender isOn]
     forKey:MyPrefKey];

The switch state is recalled in viewDidLoad with

 BOOL switchState =[[NSUserDefaults standardUserDefaults]boolForKey:MyPrefKey];
 [mySwitch setOn:switchState];

This works perfect, the position of the UISwitch is remembered.

For enabling the textfield when the switch changed

I tried as suggested:

 [[NSUserDefaults standardUserDefaults]
     setObject:[NSNumber numberWithBool:[myTextField isEnabled]]
     forKey:MyPrefKey];

and recalled with

BOOL mtf  =[[NSUserDefaults standardUserDefaults]boolForKey:MyPrefKey];
[myTextField setEnabled:mtf];

Which also worked fine.
However, only one of these work at the time. How do I make more defaults to be set at the same time? Do I have to make several PrefKeys? or can all of them be stored in the same prefKey?

Finally I tried to change the textcolor with NSUserDefaults

[[NSUserDefaults standardUserDefaults]
     setObject: [myTextField textColor]
     forKey: MyPrefKey];

that causes a crash when recalled with

UIColor*newColor= [[NSUserDefaults standardUserDefaults] objectForKey: EfloraSharePrefKey];
[myTextField setTextColor:newColor];

So how do I use NSUserDefault with textColor? I would appriciate any good solutions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:39:24+00:00Added an answer on June 9, 2026 at 8:39 pm

    You can save property list objects only to NSUserDefaults. You have to somehow serialize your color. A possible solution:

    CGFloat r, g, b, a;
    [myTextField.textColor getRed:&r green:&g blue:&b alpha:&a];
    NSNumber *rn, *gn, *bn, *an;
    rn = [NSNumber numberWithFloat:r];
    gn = [NSNumber numberWithFloat:g];
    bn = [NSNumber numberWithFloat:b];
    an = [NSNumber numberWithFloat:a];
    NSDictionary *color = [NSDictionary dictionaryWithObjectsAndKeys:
        rn, @"red",
        gn, @"green",
        bn, @"blue",
        an, @"alpha",
        nil];
    [[NSUserDefaults standardUserDefaults] setObject:color forKey:@"TextColor"];
    

    And to retrieve it:

    NSDictionary *color = [[NSUserDefaults standardUserDefaults] objectForKey:@"TextColor"];
    NSNumber *rn, *gn, *bn, *an;
    rn = [color objectForKey:@"red"];
    ...
    UIColor *c = [UIColor colorWithRed:[rn floatValue]
        green:[gn floatValue]
        blue:[bn floatValue]
        alpha:[an floatValue]];
    textField.textColor = c;
    

    If you want to be really elegant, you can wrap this into a category and even make UIColor NSCoding-comformant (in order to be able to directly persist it to the user defaults plist).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loging screen with a UISwitch for letting the user choose remember
I have a tableview with UISwitch in each row of the table. Switch is
I have a problem with UISwitch repositioning on device rotation. the switch is part
I have a UISwitch I am working with on my storyboard, and I am
I have a table view with a custom cell ( UISwitch on every cell
I have a UISwitch inside a custom UITableViewCell (the subclass of which I call
Suppose you have 2 UISwitch objects placed onto your storyboard. Each of them can
I have defined a custom cell with an UISwitch control, is the GetCell method
I have a TableViewCell with a UISwitch in accessoryView. I don't want that blue
I have a screen with several UISwitch controls. On my iPhone simulator this screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.