【问题标题】:UISwitch issue with NSUserDefaults ( cannot retrieve Bool Value )NSUserDefaults 的 UISwitch 问题(无法检索 Bool 值)
【发布时间】:2014-11-20 22:04:28
【问题描述】:

大家好,我的代码有一个奇怪的问题,我创建了一个单例类来从 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];
}

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

【问题讨论】:

  • updateUnlimitedFiles 中的 NSLog 是否有效?
  • 是的,它可以正常工作
  • 到底是什么问题?
  • 当我打开 UISwitch 并重新加载表数据或离开视图控制器并重新打开它(开关关闭)时,我说 UISwitch 无法检索 NSUserDefaults Bool 值的问题

标签: ios objective-c nsuserdefaults uiswitch


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-09-08
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多