2014-11-20 5 views
0

안녕하세요. 내 코드에 이상한 문제가 있습니다. 단 하나의 클래스를 만들어 NSUserDefaults에서 값을 검색했습니다. 여기 NSUserDefaults에 대한 UISwitch 문제 (Bool 값을 검색 할 수 없음)

.m 파일 코드 .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 

이며, 여기

#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 me THOD 내가 코드

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

을 생성하지만 .. 전혀이 문제를 해결하기 위해 어떤 생각을 작동하지 않는 이유는 무엇입니까? 자신 (계약 실수)에 의해 고정

+0

는 NSLog합니까 'updateUnlimitedFiles'에서 작동합니까? – NobodyNada

+0

예 문제없이 작동합니다. – iMokhles

+0

정확히 무엇이 문제입니까? – NobodyNada

답변

0

는 :) 내가 그

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

처럼이 곳으로

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

을 배치하고 완벽하게 일했다 .. 어쨌든 고맙습니다 @NobodyNada