0

I need to change the images/colors (whichever is easier) of buttons in a view different from the one the button is located. Any ideas? This is for iOS and I have been trying for days to get this to work. Thanks!

Abizern
  • 146,289
  • 39
  • 203
  • 257
rreichel
  • 794
  • 3
  • 9
  • 25

1 Answers1

0

I don't know if this is the proper way to do this (kinda depends on the context), but you could you us user defaults:

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *fooButtonColor = @"red";
    [prefs setObject:fooButtonColor forKey:@"fooButtonColor"];

You would put this code in the .m file of the class where you want to "set" the color. In

Then in the other view, get the color from the defaults and check e.g. by doing a string comparison:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:fooButtonColor forKey:@"fooButtonColor"];
NSString *fooButtonColor = [prefs objectForkey:@"fooButtonColor"];
if ([fooButtonColor isEqualtoString @"red"]) {
foobutton.backgroundColor = [UIColor redColor];
}

Probably more efficient would be to use rgba values. Check out this question: Saving UIColor to and loading from NSUserDefaults

Good luck

Community
  • 1
  • 1
Thomas K
  • 6,076
  • 5
  • 39
  • 56
  • Thanks man. When you have the "redColor" is that something that Xcode will recognize? Also, where in my code should I put this? In the .h or .m file of a view controller or in the app delegate. Sorry, I've tried to understand apple's instructions but well, they aren't very well written. Thanks again! – rreichel Aug 12 '11 at 16:35
  • Also, the view that I am setting the colors from is a subview of the other view (the one that is getting its colors set) in case that helps at all. – rreichel Aug 12 '11 at 16:37
  • Well no. But when you set the backgroundcolor of your button you would e.g. do this: `myButton.backgroundColor = [UIColor redColor];`. I'll also edit my answer – Thomas K Aug 12 '11 at 16:40
  • Here is how I call the settings view controller: 'SettingsViewController *settingsView = [[SettingsViewController alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:settingsView animated:YES];' What else are you looking for in code? Thanks so much for your help so far! – rreichel Aug 12 '11 at 16:48
  • So you've got SettingsViewController.m right? That's where you should put the first snippet. How does your user assign the color in your SettingsViewcontroller? With a picker, button or ..? – Thomas K Aug 12 '11 at 16:54
  • With a button, and yes I have the SettingsViewController.m – rreichel Aug 12 '11 at 18:00
  • Ok do you have a method that get's called when you press the button? – Thomas K Aug 12 '11 at 18:16
  • No, I don't believe that I do. – rreichel Aug 12 '11 at 18:19
  • Sorry I just realized I haven't been tagging you! @Thomas K – rreichel Aug 12 '11 at 18:36
  • you know what.. This is not a really efficient way to solve this issue (thru comments). Does you xcode project contain any sensitive data? if not you could upload your project folder somewhere and I could fix it up for you and you could study what I did. Would you like that? – Thomas K Aug 12 '11 at 18:56
  • Eh about the tagging AFAIK you don't have to. It's just handy when you have multiple commenters – Thomas K Aug 12 '11 at 18:58