从 UITableView UISwitch 写入每个项目的 Plist 键值

Write to Plist key value per item, from UITableView UISwitch

我有一个 UITableView,通过可编辑的 plist 从 NSMutableDictionay 填充,不在主包中。我可以 read/write/add 到 plist。

UITableViewUISwitches。 plist 是 root/dictionary,有 6 个根键,每个包含 4 个项目,

我的钥匙是(示例)

theball     Array    (4 items)
  item 0    string   stringxssd
  item 1    string   stringgad
  item 2    string   stringgad
  item 3    string   stringrad
theswitch   Array    (4 items)
  item 0    Boolean  NO
  item 1    Boolean  YES
  item 2    Boolean  NO
  item 3    Boolean  NO

当用户转动开关时 on/off YES/NO。我如何写入 plist 并仅将 / 让我们说 theswitch 键的第 2 项,布尔值更改为 YES?到目前为止,我无法具体更改键项的布尔值,只能更改键本身吗?我想在每次开关更改时更改每个项目...

您不能将布尔值保存到 plist,它必须是整数或字符串我直接从 IBOutlet UISwitch 变量使用此代码:

     [plistDict setValue:[NSString stringWithFormat:@"%d",UI_swictch1.on] forKey:@"UI_switch"];

这会更新 NSMutable Dict - 当然您需要将 plist 作为文件写回,我假设您有相应的代码。我假设你知道如何做剩下的事情。

试试这个

[myDictionary setObject: [NSNumber numberWithBool: switch.isOn ] forKey:@"someKey"];

然后像这样解压

BOOL someBool = [[myDictionary objectForKey: @"someKey"] boolValue ];

所以试试像这样的切换动作..

-(IBAction) switchAction:(id)sender{

NSInteger swNum = [sender tag];
UISwitch swtch = (UISwitch *)sender;
BOOL theSetting = swtch.isOn;

//assuming a NSMutableDictionary property which was loaded from plist file at launch
//let us call this dictionary self.model  ..

NSArray *theBooleans = [self.model objectForKey: @"theswitch" ];
NSMutableArray *boolsMutable = [NSMutableArray arrayWithArray: theBooleans ];
[boolsMutable replaceObjectAtIndex:swNum withObject: [NSNumber numberWithBool:theSetting]];

[self.model setObject:boolsMutable forKey: @"theswitch" ];
}

对于那些感兴趣的人。感谢 Jef,我将他的代码与我已经拥有的 ibaction 调用一起使用....,

-(IBAction) switchAction:(id)sender{
    NSLog(@"*MVC* switchAction ");
    NSInteger swNum = [sender tag];
    Switches = (UISwitch *)sender;
    BOOL theSetting = Switches.isOn;

    NSArray *theBooleans = [self.TheDict objectForKey: @"UserSwitch" ];
    NSMutableArray *boolsMutable = [NSMutableArray arrayWithArray: theBooleans ];
    [boolsMutable replaceObjectAtIndex:swNum withObject: [NSNumber numberWithBool:theSetting]];

    [self.TheDict setObject:boolsMutable forKey: @"UserSwitch" ];
    [TheDict writeToFile:PlistLocation atomically:YES];
    NSLog(@"*MVC* switchAction COMPLETE");

}

在我的 cellForRowAtIndexPath 中,开关是在构建 table 时生成的,并且开关是从 plist 中给定的标签。

NSNumber *tagNumber = [SwitchTag objectAtIndex:indexPath.row];
    NSInteger blabla = [tagNumber integerValue];
// add the switchs
    Switches = nil;
    Switches = [[UISwitch alloc] initWithFrame:CGRectZero];
    Switches.tag = blabla;
    NSLog(@"*MVC* Switches.tag: %d", blabla);
    cell.accessoryView = Switches;
    BOOL switchState;
    switchState = [[theSwitch objectAtIndex:indexPath.row] boolValue];
    if (switchState == TRUE)
    {
        [Switches setOn:YES animated:YES];
    }
    else
    {
        [Switches setOn:NO animated:YES];
    }
    [Switches addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

..杰夫,你的男人。感谢您提供线索、代码和快速回答。