NSUserDefaults的UISwitch问题(无法检索Bool值)

时间:2014-11-20 22:04:28

标签: ios objective-c nsuserdefaults uiswitch

大家好我的代码有一个奇怪的问题我创建了一个单例类来从NSUserDefaults检索值。

这是.h代码

#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 

这是我的.m文件代码

#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

尝试检索Bool值并将其设置为UISwitch,如下所示但不起作用

- (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方法我创建了代码

- (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];
}

但它根本不起作用.. 有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我自己修正(安排错误):) 我放了

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

到这个地方

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];
}

并且工作完美..无论如何Thanx @NobodyNada