1

我正在使用开关控件,每当我从页面导航或重新启动应用程序时,它的值都会设置为默认“关闭”。如何克服它,以便如果按下它应该保持打开或者如果按下它应该保持关闭,直到我们手动更改?

这是我为滚动视图分配和设置 ffram 的代码

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

}
}
}

这是我的切换操作方法:

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

}
4

3 回答 3

2

那么同样你需要保存开关的状态。为此,您甚至可以使用数据库或 NSUserDefaults。使用 NSUserDefaults 会好很多。

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

    }
}

然后在您的 viewwillAppear 方法中执行以下操作

-(void)viewWillAppear:(BOOL)animated
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"switch"]isEqualToString:@"on"]) {

switch.Ison=YES;

    }
else
{
switch.Ison=NO;
}
}
于 2011-05-21T09:07:44.323 回答
0

您应该有一个值来保存用户偏好的开关控制状态。当你导航到这个视图时,你应该检查这个值。

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

阅读和写入用户首选项请参考以下问题: iphone-reading-and-saving-preferences

于 2011-05-21T06:14:09.443 回答
0

您只需使用该方法来设置开关,就像这样

- (void)switchAction:(UISwitch*)sender
{
    DEBUGLOG(NSLog(@"switchAction: sender = %d, isOn %d", [sender tag], [sender isOn]));

    if(sender.on)
    {

    }
    else
    {

    }
}

如果你想将它设置为默认值,那么你只需要在你的应用程序中设置它,比如在 viewwillappear 的实现文件中,或者在实现之后在你的文件顶部

switch.on=YES;
于 2011-05-21T07:43:37.297 回答