Question

I recently found how to program a switch and how to change the background from 1 view.

My question is how to change the background of multiple views (UIImage) when changing the value of the switch.

For example: It's a preference page and when I switch the background of the preferences page changes so the switch works.

Now i want the background (UIImage) of my first view named "viewcontroller" to change to the same background as the background of my preferences page.

Was it helpful?

Solution

in my opinion, there may be a lot of different ways to achieve this. What i would do is save the image name string into [NSUserDefaults standardUserDefaults].

Such as when the switch value changes, you call

    [[NSUserDefaults standardUserDefaults] setObject:@"BackgroundImageName" forKey:@"BackgroundImageOne.jpg"];
    [[NSUserDefaults standardUserDefaults]synchronize];

and when you set up the background, you could do

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    UIImage *bgImage;
    if ([userDefault objectForKey:@"BackgroundImageName"])
        bgImage = [UIImage imageNamed:[userDefault objectForKey:@"BackgroundImageName"]];
    else
        bgImage = [UIImage imageNamed:@"DEFAULT_NAME"];

    //... Set the bgImage to your background image view ...//

Maybe you have to do some changes based on the actual usage. But hope this give you some hints.

OTHER TIPS

One of possible solutions would be to create a public property of application delegate holding your background image.

@property (strong) UIImage *globalBackground;

In your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions you should initialize it either to some default image or last saved image.

self.globalBackground = [UIImage imageNamed:@"defaultBackground"];

Let's say your appDelegate header file is MyAppDelegate.h. You should import it in all the classes that use this default background image mechanism. Or simply add it to your Prefix.pch file.

In your settings view controller you would then set:

//your background setting handler
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.globalBackground = ... //however you get the selected image
//and here you set the background image of settings view controller

Then in each of the view controllets viewWillAppear: you set its background in similar manner:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//then you use appDelegate.globalBackground the same way you are doing it now

If you have a lot of view controllers you might want to consider creating a subclass of UIViewController that implements this mechanism (possibly calling it UIViewControllerWithBackground) and make all your view controllers subclasses of this class.

Note A: one would (instead of appDelegate's property) typically use a singleton class holding all the settings. It might be considered as a bad (quick & dirty) practice. More...

Note B: for saving the name of last selected background you could use NSUserDefaults. This is out of the scope of this question and there is a lot of sample code available out there...

Is it possible to pass the image, which I saved in Xcode, to a next viewcontroller? Or give it a number and pass that number to a next viewcontroller? For example, when switchone is on, NSNumber = 1, pass it to the next view, and then if (NSNumber == 1) than display "backgroundone.jpg"???

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top