1

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.

Swati
  • 2,870
  • 7
  • 45
  • 87
Sam VG
  • 143
  • 1
  • 2
  • 9
  • you could create a globally available variable (could be a singleton or - not so clean - a public property of app delegate) that holds the background image and each view(controller) would refresh it's background the moment before it becomes visible (appears) – Rok Jarc Feb 02 '14 at 13:16
  • could you give me a code example please??? I'm not so good in Xcode yet... – Sam VG Feb 02 '14 at 13:34
  • O.k. If you want a more detailed answer you can add the code you use to set the background now ti your question. – Rok Jarc Feb 02 '14 at 14:40

3 Answers3

1

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.

Xu Yin
  • 3,932
  • 1
  • 26
  • 46
0

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...

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
0

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"???

Sam VG
  • 143
  • 1
  • 2
  • 9