UISwitch不会改变状态
我有一些UISwitches,我想从.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];
调用它[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方法,我试图改变我的UISwitch在我的loadVariables方法的状态。 如果我把代码改变UISwitch状态在ViewDidLoad它的作品。 但代码似乎并没有从我的[[Settings的sharedInstance] loadVariables]方法执行。
你有没有尝试在viewWillAppear
设置开关? 这在我看来是最合适的地方。
另外,似乎有两个loadVariables
方法的实例。 单身人士不能/不应该包含开关。 但是在viewDidLoad
你调用了单例的方法。 这可能是你的问题的一部分。 从你的单身人士获取值,并在视图控制器中设置。
据推测, dictionary
是零,或者它不包含任何对象,你正在给的关键。
当你的视图已经被创build时,你是否执行这个代码? 如果您在viewDidLoad调用之前执行此操作,则您的开关可能会等于零,因此您的代码将不会更改其状态。