ios - UISwitch 不改变状态

标签 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 的状态。如果我确实将用于更改 UISwitch 状态的代码放在 ViewDidLoad 中,它就可以工作。但是代码似乎没有从我的 [[Settings sharedInstance] loadVariables] 方法中执行。

最佳答案

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

此外,您的 loadVariables 方法似乎有两个实例。单例不能/不应该包含开关。但是在 viewDidLoad 中你调用了单例的方法。这可能是您问题的一部分。从单例中获取值并在 View Controller 中设置。

关于ios - UISwitch 不改变状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23986150/

相关文章:

ios - UICollectionViewCell 大小不正确

xcode - 在 Swift 中设计计算器 - 如何在需要输入 float 时添加单个 "."?

ios - 编译项目时是否可以检测到TARGET

ios - 本地通知设置特定的开火日期

ios - iCloud + 预加载的 CoreData

iphone - 具有 setDelegate 和 action 的 UIGestureRecognizer

objective-c - cocoa : Action 处理

objective-c - ios:防止 ScrollView 缩放超出范围

ios - 移动一个框架的父大小的一半将它一直移动(坐标加倍)?

ios - UIScrollView 的 contentOffset 在添加小数字时被截断