Passing Data Through ViewControllers - Make Settings Page

So, I've had this problem for a while, and if someone can lead me on the right track, I'd really appreciate it. Basically I am building an app, but with a settings button. Whenever I turn on my SegmentedControl or UISWITCH, I revert back to it by default. Also, I want to have a Segmented control for changing colors. I have set the colors, but all the information I want to change is in controller # 1. Basically how to make the settings page to save the changes. so far my code.

- (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 when you see "appTitle" is on the first view controller, so it doesn't automatically work. How can I fix this. please connect me somewhere / show me not difficult. (I will have many shortcuts too)

+3


source to share


2 answers


For this purpose, I usually use a file-level structure (not nested in any class). Thus, it must be available throughout the application. Set the value when changing 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 View Controller settings viewDidLoad:



Controller.selectedSegmentIndex = settings.selSeg

      

In IBAction for segmented control:

settings.selSeg = Controller.selectedSegmentIndex

      

+1


source


If you want to create a settings page, I would suggest you read it NSUserDefaults

. What you can do is create defaults in didFinishLaunchingWithOptions

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 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 can create storyboard TableViewController

and create static cells tableView

. You can create sections for cells in the Attribute Inspector tab in the Interface Builder. Then you can drag and drop shortcuts, radio buttons, etc. And connect them to the class PreferencesTableViewController

you need to create.

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

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

      



and you call it like this:

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

      

You can set defaults for many things, as you will see in the Apple documentation

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

You will find a post about UIColor

s here that seems to focus on you.

Saving UIColor and loading from NSUserDefaults

0


source







All Articles