3

So I've been having this problem for a while now and If anyone can bring me onto the right path I would greatly appreciate it. Basically I am making an app but with a settings button. Whenever I enable my SegmentedControl or UISWITCH, and I go back to that back its back to default. Also I want to have a Segmented control to change the color. I have set the colors but all the info I want to change is on view controller #1. Basically how do I make a settings page to keep the changes. heres my code so far.

- (IBAction)colorController:(id)sender {

if (Controller.selectedSegmentIndex == 0) {

    //App title text color
     appTitle.textColor =  [UIColor colorWithRed:1.00 green:1.00 blue:0.00 alpha:1.0];

    //Background color when selected
    Controller.tintColor = [UIColor colorWithRed:1.00 green:1.00 blue:0.00 alpha:1.0];

    //The font of the selected
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor blackColor],NSForegroundColorAttributeName,
                               nil];
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected];



}
if (Controller.selectedSegmentIndex == 1) {

    //App title text color
    appTitle.textColor = [UIColor colorWithRed:0.00 green:0.66 blue:1.00 alpha:1.0];

    //Background color when selected
    Controller.tintColor = [UIColor colorWithRed:0.00 green:0.66 blue:1.00 alpha:1.0];

    //The font of the selected
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor whiteColor],NSForegroundColorAttributeName,
                               nil];
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected];

}
if (Controller.selectedSegmentIndex == 2) {

    //App title text color
    appTitle.textColor = [UIColor colorWithRed:0.98 green:0.22 blue:0.22 alpha:1.0];

    //Background color when selected
    Controller.tintColor = [UIColor colorWithRed:0.98 green:0.22 blue:0.22 alpha:1.0];


    //The font of the selected
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor whiteColor],NSForegroundColorAttributeName,
                               nil];
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected];
}
if (Controller.selectedSegmentIndex == 3) {

    //App title text color
    appTitle.textColor = [UIColor colorWithRed:0.15 green:0.82 blue:0.44 alpha:1.0];

    //Background color when selected
    Controller.tintColor = [UIColor colorWithRed:0.15 green:0.82 blue:0.44 alpha:1.0];

    //The font of the selected
    NSDictionary *fontColor = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [Controller setTitleTextAttributes:fontColor forState:UIControlStateSelected];

}

}

Now as you can see "appTitle" is on the first view controller so it automatically don't work. How can I fix this. please link me somewhere / show me in a not complicated way. (I will have lots of labels too)

Dynamic
  • 61
  • 5

2 Answers2

1

For this purpose, I usually use a struct declared at the file level (not nested in any sort of class). Thus, it should be accessible throughout the app. Set the value when you change the selected segment; get the value when loading the view controller. Here's an example in Swift:

struct setStruct {
    var selSeg: Int = 0
}
var settings = setStruct()

In the settings View Controller viewDidLoad:

Controller.selectedSegmentIndex = settings.selSeg

In the IBAction for the segmented control:

settings.selSeg = Controller.selectedSegmentIndex
BradzTech
  • 2,755
  • 1
  • 16
  • 21
0

If you want to make a "settings" page, I would suggest you read up on NSUserDefaults. What you could do is create default values in the didFinishLaunchingWithOptions of AppDelegate.m.

Here's an example:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // this is a method I created to check if there are default values set already
    [self checkUserDefaults];
    return YES;
}

#pragma mark - Initial defaults

- (void)checkUserDefaults {

    NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];
// If there's a default value for a key, then don't do anything
    if([[[defaults dictionaryRepresentation] allKeys] containsObject:@"defaultTime"]){
        NSLog(@"User Defaults are ALREADY set");
    } else {
// Otherwise, create a default value for it

        // ** Set initial userDefaults **
        [[NSUserDefaults standardUserDefaults]setInteger:15 forKey:@"defaultTime"];

        NSLog(@"User Defaults have been initially set");
        }
}

In Interface Builder, you could create a TableViewController storyboard and make the tableView content Static Cells. You could create sections for the cells on the Attributes Inspector tab in Interface Builder. Then you could drag in Labels, Switches, etc. and wire them up to a PreferencesTableViewController class that you'd need to create.

Basically, you create a default value that persists between launches with this:

[[NSUserDefaults standardUserDefaults]setInteger:15 forKey:@"defaultTime"]

and you call it like this:

[[NSUserDefaults standardUserDefaults]valueForKey:@"defaultTime"]

You can set default values for many things, as you'll see in Apple's documentation

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html

Here's a post dealing with UIColors, which seems to be what you're primarily focused on.

Saving UIColor to and loading from NSUserDefaults

Community
  • 1
  • 1
Adrian
  • 16,233
  • 18
  • 112
  • 180