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 ?
Fixed by myself ( arrangements mistake ) :) i placed
to this place like that
and worked perfect .. anyway Thanx @NobodyNada