Reputation: 45
in this app im trying to make the buttons of sound and vibration switch between two images i.e grey and color depending on whether sound is on or not. When it is clicked it should flip over the image and go from grey to color . i have managed to make it flip once that is from color to grey however it doesnt go back to color when i click it again , please help me figure this out. thanks in advance. this is my AppDelegate.m
if(![defaults objectForKey:@"firstTime"])
{
//Application is running the first time
//Do first time stuff
[defaults setBool:NO forKey:@"firstTime"];
[defaults setBool:YES forKey:@"musicKey"];
[defaults setBool:YES forKey:@"soundKey"];
[defaults setBool:YES forKey:@"vibrationKey"];
[defaults setBool:YES forKey:@"linkKey"];
[defaults synchronize];
}
else{
[defaults setBool:YES forKey:@"firstTime"];
}
this is my settingsviewcontroller where the actual flipping will happen:
-(void)viewDidLoad
{ [super viewDidLoad];
[self checkValues];
[self makeSettingsPage];
self.scrollView.contentSize=[_outerView frame].size;
}
-(void)makeSettingsPage
{
RoundButton *about=[[RoundButton alloc]initWithColor:color1 WithText:@"ABOUT"];
RoundButton *privacyPolicy=[[RoundButton alloc]initWithColor:color2 WithText:@"PRIVACY \n POLICY"];
RoundButton *termsOfUse=[[RoundButton alloc]initWithColor:color2 WithText:@"TERMS \n OF USE"];
about.center=CGPointMake(29, 451);
privacyPolicy.center=CGPointMake(29, 481);
termsOfUse.center=CGPointMake(29, 511);
[self.scrollView setContentMode:UIViewContentModeScaleAspectFit];
self.scrollView.frame= self.view.frame;
// [scrollView addSubview:viewToAdd];
// scrollView.contentSize = viewToAdd.frame.size;
//
//Content Size is size of the view inside the scrollview
self.containerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"background_letter.png"]]];
[self.containerView addSubview:about];
[self.containerView addSubview:privacyPolicy];
[self.containerView addSubview:termsOfUse];
[self.scrollView addSubview:_containerView];
// self.scrollView.contentSize;_containerView.frame.size;
[self.outerView addSubview:self.scrollView];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (IBAction)musicAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool isMusicOn=[defaults boolForKey:@"musicKey"];
if (!isMusicOn) {
UIImage *btnImage1 = [UIImage imageNamed:@"musicgrey.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"musicColor.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
}
- (IBAction)soundAction:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool isItOn=[defaults boolForKey:@"soundKey"];
if (!isItOn) {
UIImage *btnImage1 = [UIImage imageNamed:@"soundcolors.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"soundgrey.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
}
- (IBAction)vibrationAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool isItOn=[defaults boolForKey:@"vibrationKey"];
if (!isItOn) {
UIImage *btnImage1 = [UIImage imageNamed:@"virationColor.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"vibrationgrey.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
}
- (IBAction)pushAction:(id)sender {
}
- (IBAction)linkAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
bool isItOn=[defaults boolForKey:@"linkKey"];
if (!isItOn) {
UIImage *btnImage1 = [UIImage imageNamed:@"colorfulfacebook.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"grayfacebook.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
}
}
-(void)checkValues
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:@"musicKey"])
{
//Load Active Image for it
[self.musicButton setImage:[UIImage imageNamed:@"musicColor.png"]
forState:UIControlStateNormal];
}
else{
//Load Gray Image
[self.musicButton setImage:[UIImage imageNamed:@"musicgrey.png"]
forState:UIControlStateNormal];
}
if([defaults boolForKey:@"soundKey"])
{
//Load Active Image for it
[self.soundButton setImage:[UIImage imageNamed:@"soundcolors.png"]
forState:UIControlStateNormal];
}
else{
//Load Gray Image
[self.soundButton setImage:[UIImage imageNamed:@"soundgrey.png"]
forState:UIControlStateNormal];
}
if([defaults boolForKey:@"vibrationKey"])
{
//Load Active Image for it
[self.vibrationButton setImage:[UIImage imageNamed:@"virationColor.png"]
forState:UIControlStateNormal];
}
else{
//Load Gray Image
[self.vibrationButton setImage:[UIImage imageNamed:@"vibrationgrey.png"]
forState:UIControlStateNormal];
}
if([defaults boolForKey:@"linkKey"])
{
//Load Active Image for it
[self.linkButton setImage:[UIImage imageNamed:@"colorfulfacebook.png"]
forState:UIControlStateNormal];
}
else{
//Load Gray Image
[self.soundButton setImage:[UIImage imageNamed:@"grayfacebook.png"]
forState:UIControlStateNormal];
}
}
@end
Upvotes: 0
Views: 47
Reputation: 131503
That's an awful lot of code to post without telling us what part isn't working as intended. You should direct us to a specific method.
I gather you're talking about ALL your IBAction methods except pushAction (which is blank.)
You don't have any code to change the state of the switch in user defaults, so each time your code executes, your if statement does the same thing.
You need to add a line to flip the bool. Something like this:
NSString *key = @"musicKey"
bool isMusicOn=[defaults boolForKey: key];
[defaults setBool: !isMusicOn] forKey: key];
Also take note of Matt's suggestion about registerDefaults. That is a much better way to set starting default values (search on "registerDefauts" in the Xcode docs.
Finally, I would suggest collapsing all your button code into 1 action method. Add tag values to all your buttons. In viewDidLoad, set up an array of key strings. In your IBAction method, get the tag value of the button using sender.tag
and use it to index into the array of key strings.
(As a general rule, when you find yourself duplicating the same code over and over with only a few things changing, it's a sign that you should re-think your approach. Copy-pasting the same code over and over is tedious, hard to maintain, and error-prone.)
Upvotes: 0
Reputation: 536026
For one thing, this is NOT how to set default defaults:
if(![defaults objectForKey:@"firstTime"])
Instead, call registerDefaults:
. That's what it's for.
Upvotes: 1