UISwitch不改变状态

时间:2014-06-02 01:18:18

标签: ios xcode

我有一些UISwitch,我想从.plist文件加载他们的状态。我已经尝试了无数次但似乎没有工作。这是我的代码:

-(void) loadVariables {
    NSMutableDictionary *dictionary;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Settings.plist"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
        plistPath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
        NSLog(@"NOT FOUND%@",plistPath);
    }

    dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

    [[Settings sharedInstance] setMobileSwitch:[[dictionary objectForKey:@"mobileSwitch"]boolValue]];

    [[Settings sharedInstance] setEmailSwitch:[[dictionary objectForKey:@"emailSwitch"]boolValue]];

    [[Settings sharedInstance] setLocationSync:[[dictionary objectForKey:@"locationSync"]boolValue]];

    [[Settings sharedInstance] setCaptureLocation:[[dictionary objectForKey:@"captureLocation"]boolValue]];

    [self.mobileSwitchButton setOn:[[dictionary objectForKey:@"mobileSwitch"]boolValue]];
    [self.emailSwitchButton setOn:[[dictionary objectForKey:@"emailSwitch"]boolValue]];
    [self.locationSyncButton setOn:[[dictionary objectForKey:@"locationSync"]boolValue]];
    [self.captureLocationButton setOn:[[dictionary objectForKey:@"captureLocation"]boolValue]];
}

由于某些原因,当我在我的viewDidLoad方法(如[self.mobileSwitchButton setOn:1];)中调用它时,它可以正常工作。我做错了什么?

在NSLog中,我的字典打印出以下内容:

dictionary {
    captureLocation = 0;
    emailSwitch = 1;
    locationSync = 1;
    mobileSwitch = 0;
}

在我的ViewDidLoad方法中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [mobileSwitchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
    [emailSwitchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
    [locationSyncButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
    [captureLocationButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];
    [[Settings sharedInstance] loadVariables];
}

在我的.h文件中,我也有这个:

//Switches
@property (retain, nonatomic) IBOutlet UISwitch *mobileSwitchButton;
@property (retain, nonatomic) IBOutlet UISwitch *emailSwitchButton;

@property (retain, nonatomic) IBOutlet UISwitch *locationSyncButton;
@property (retain, nonatomic) IBOutlet UISwitch *captureLocationButton;

在我的实现文件中,我也有:

+(Settings *)sharedInstance {
    static Settings *myInstance = nil;

    // check to see if an instance already exists
    if (nil == myInstance) {
        myInstance  = [[[self class] alloc] init];
    }
    // return the instance of this class
    return myInstance;
}

我在我的ViewDidLoad方法中调用了loadVariables方法,并且我正在尝试在loadVariables方法中更改我的UISwitch的状态。如果我在ViewDidLoad中放置用于更改UISwitch状态的代码,它就可以工作。但是代码似乎没有从我的[[Settings sharedInstance] loadVariables]方法执行。

3 个答案:

答案 0 :(得分:2)

据推测,dictionary为零,或者它不包含您所提供的密钥的任何对象。

答案 1 :(得分:2)

您是否尝试过设置viewWillAppear中的开关?在我看来,这是最合适的地方。

此外,您的loadVariables方法似乎有两个实例。单例不能/不应包含开关。但在viewDidLoad中你称之为单身人士的方法。这可能是您问题的一部分。从单例中获取值并在视图控制器中设置。

答案 2 :(得分:1)

您的观点已经创建后,您是否执行此代码?如果您在viewDidLoad调用之前执行此操作,则您的开关可能等于nil,因此您的代码不会更改其状态。