Reputation: 1845
I was working with the switch control, its value sets to default 'off' whenever I navigate from the page or restart the application. How to over come that, so that if pressed on it should stay on or if pressed off it should stay off, until we change manually?
Here is my code where I'm allocating and setting ffram for scrollview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell new";
listData =[self.tableContents objectForKey:[self.sortedgroups objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
if(indexPath.section==0)
{
switch (indexPath.row)
{
case 0:
{
switcher = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[switcher addTarget:self action:@selector(switchAction:)
forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switcher;
switcher.tag = indexPath.row;
//cell.textLabel.text=@"switcher";
}
break;
case 1:
{
slide = [[[UISlider alloc] initWithFrame:CGRectMake(0,0, 150, 15)] autorelease];
[slide addTarget:self action:@selector(slideact:)
forControlEvents:UIControlEventValueChanged];
cell.accessoryView = slide;
// slide.tag = indexPath.row;
}
break;
}
}
}
This is my method for switch action:
- (void)switchAction:(UISwitch*)sender
{
NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]);
if(sender.on)
{
[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switcher"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switcher"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switcher"]isEqualToString:@"on"])
{
switcher.on=YES;
}
else
{
switcher.on=NO;
}
}
Upvotes: 1
Views: 2445
Reputation: 2592
Well sameer then you need to save the state of switch. For this you can even use the database or NSUserDefaults. Using NSUserDefaults would be pretty much better.
- (void)switchAction:(UISwitch*)sender
{
DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));
if(sender.on)
{
[[NSUserDefaults standardUserDefaults]setObject:@"on" forKey:@"switch"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults]setObject:@"off" forKey:@"switch"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
Then in your viewwillAppear method do the following
-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {
switch.Ison=YES;
}
else
{
switch.Ison=NO;
}
}
Upvotes: 2
Reputation: 2592
You simply use the method to set the switch on and off like this
- (void)switchAction:(UISwitch*)sender
{
DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));
if(sender.on)
{
}
else
{
}
}
And if you want to set it on default then you just need to set it on in your application say in the implementation file in viewwillappear or at the top of your file after implementaion as
switch.on=YES;
Upvotes: 0
Reputation: 1595
You should have a value to save the switch control status may on user preferences. When you navigate to this view you should check for this value.
Boolean status=YES; // You should read this value from User Preference for example it represent the last status set to the switch control.
[switchView setOn:status animated:NO];
Refer to the following question for reading and writing to User Preferences: iphone-reading-and-saving-preferences
Upvotes: 0