UISwitch issue with NSUserDefaults ( cannot retrieve Bool Value )

93 views Asked by At

Hi all i have a strange issue with my code i created a singleton class to retrieve values from NSUserDefaults.

here is the .h code

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

/** Global project settings */
@interface MYAPPSettings : NSObject {
    NSUserDefaults *MYAPPDefaults;
}

@property NSUserDefaults *MYAPPDefaults;
+ (MYAPPSettings *)sharedInstance;
- (void)setUnlimitedFiles:(BOOL)anUnlimitedFiles;
- (BOOL)unlimitedFiles;
@end 

and here is my .m file code

#import "MYAPPSettings.h"

#define UnlimitedFiles_KEY @"unlimFiles"
@implementation MYAPPSettings

@synthesize MYAPPDefaults;
+ (MYAPPSettings *)sharedInstance
{
    static MYAPPSettings *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[MYAPPSettings alloc] init];
    });

    return _sharedInstance;
}
- (id)init {
    if (self = [super init]) {
        MYAPPDefaults = [NSUserDefaults standardUserDefaults];
        [MYAPPDefaults synchronize];
    }
    return self;
}
- (void)setUnlimitedFiles:(BOOL)anUnlimitedFiles {
    [MYAPPDefaults setBool:anUnlimitedFiles forKey:UnlimitedFiles_KEY];
    [MYAPPDefaults synchronize];
}

- (BOOL)unlimitedFiles {
    return (BOOL)[MYAPPDefaults boolForKey:UnlimitedFiles_KEY];
}
@end

the trying to retrieve Bool value and set it to UISwitch like following but doesn't work

- (void)updateUnlimitedFiles {
   BOOL state = [filsSwitch isOn]; // filesSwitch is UISwitch created inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
   [[MYAPPSettings sharedInstance] setUnlimitedFiles:state];
   NSLog(@"=============[MYAPP] %@ ON", NSStringFromSelector(_cmd));
}

cellForRowAtIndexPath method i created code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSString *SimpleTableIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)indexPath.section, (long)indexPath.row];
    //......
    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
        // [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    filesSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [filesSwitch setOn:[[MYAPPSettings sharedInstance] unlimitedFiles] animated:YES];
}

but it doesn't work at all.. any idea to fix this issue ?

1

There are 1 answers

0
iMokhles On BEST ANSWER

Fixed by myself ( arrangements mistake ) :) i placed

filesSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[filesSwitch setOn:[[MYAPPSettings sharedInstance] unlimitedFiles] animated:YES];

to this place like that

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
    // [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    filesSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [filesSwitch setOn:[[MYAPPSettings sharedInstance] unlimitedFiles] animated:YES];
}

and worked perfect .. anyway Thanx @NobodyNada