简体   繁体   English

将文本字段值存储在字典中

[英]Storing text field value in a dictionary

I have Add and Delete buttons which adds textfield in a view. 我有添加和删除按钮,可在视图中添加文本字段。 Now i want to store the textfield values in a dictionary. 现在我想将文本字段值存储在字典中。

-(void)addTextField{

keyTextField = [[UITextField alloc] initWithFrame:CGRectMake((((self.view.frame.size.width)*5)/100), yAxisDistance, (((self.view.frame.size.width)*40)/100),(((self.view.frame.size.height)*8)/100))];
keyTextField.borderStyle = UITextBorderStyleRoundedRect;
keyTextField.placeholder = @"Key Value";
keyTextField.delegate = self;
mKeyTag+=1;
keyTextField.tag = mKeyTag;
[self.view addSubview:keyTextField];

valueTextField = [[UITextField alloc] initWithFrame:CGRectMake((((self.view.frame.size.width)*55)/100), yAxisDistance, (((self.view.frame.size.width)*40)/100),(((self.view.frame.size.height)*8)/100))];
valueTextField.borderStyle = UITextBorderStyleRoundedRect;
valueTextField.placeholder = @"Value";
valueTextField.delegate = self;
mValueTag+=1;
valueTextField.tag = mValueTag;
[self.view addSubview:valueTextField];
yAxisDistance = yAxisDistance+(((self.view.frame.size.height)*13)/100);
}

-(void)deleteTextField{

if (mKeyTag>=1000) {
    UITextField *textField = (UITextField *)[self.view viewWithTag:mKeyTag];
    [textField removeFromSuperview];
    mKeyTag-=1;
    yAxisDistance = yAxisDistance-35;
}
if (mValueTag>=2000) {
    UITextField *textField = (UITextField *)[self.view viewWithTag:mValueTag];
    [textField removeFromSuperview];
    mValueTag-=1;
}
}

If i click on Add its adding two textfields one for key value and one for value for that key. 如果我单击添加,则添加两个文本字段,一个用于键值,一个用于键值。 But if i add 5 pairs of textfields by clicking Add button 5 times and give some values to those textfields and try to store in a dictionary, it is storing only the last textfields value. 但是,如果我通过单击添加按钮5次来添加5对文本字段,并为这些文本字段提供一些值并尝试存储在字典中,则它仅存储最后一个文本字段值。 But i want to store all the data provided in those 5 pairs of textfield. 但是我想存储在这5对文本字段中提供的所有数据。 for storing data i am using following code 用于存储数据,我正在使用以下代码

  NSMutableDictionary *Dict = [[NSMutableDictionary alloc]init];
  [Dict setValue:valueTextField.text forKey:keyTextField.text];

Please help as i am very new to this field. 请帮忙,因为我是这个领域的新手。

If you want a key to have multiple values, what you want is a dictionary where the value is a mutable array, and keep adding values to it. 如果您想要一个键具有多个值,则需要一个字典,其中该值是一个可变数组,并继续向其中添加值。

- (void)addValue:(id)value forKey:(id<NSCopying>)key
{
    NSMutableArray *array = self.dictionary[key];
    if (array == nil)
    {
        array = [NSMutableArray array];
        self.dictionary[key] = array;
    }
    [array addObject:value];
}

Your dictionary keys need to be unique, otherwise the values could be overwritten with a new value for that same key. 您的字典键必须是唯一的,否则该值可能会被同一键的新值覆盖。

if(!dict) { //if no instance of the dict exists, create it
    dict = [NSMutableDictionary new];
}

NSString *value = valueTextField.text; //grab your value
NSString *key = keyTextfield.text; //grab your key

//setting the value at this stage runs the risk of overwriting existing values for that key.
//[dict setValue:value forKey:key]; 

NSMutableArray *valuesForKeyArray = [NSMutableArray new]; //will hold your values for you
if ([dict valueForKey:key]) { //check if the dictionary already contains value for key

    //dictionary contains values for the key already -> grab them
    NSArray *existingValues = [NSArray arrayWithArray:[dict valueForKey:key]];
    [valuesForKeyArray addObjectsFromArray:existingValues]; //add them in
}

//add the latest value
[valuesForKeyArray addObject:value];

//save into your dictionary (either the array with existing values + new value or just the new value)
[dict setValue:valuesForKeyArray forKey:key];

You create the dictionary (if it hasn't been created elsewhere already), grab your specific key and value, create an array for holding values, check to see if the dictionary already has some values saved for that key, if so--> you add them in, then add the latest value to the array and save back into your dictionary. 您创建字典(如果尚未在其他地方创建字典),获取您的特定键和值,创建一个用于保存值的数组,检查字典是否已经为该键保存了一些值,如果是->您将它们添加到其中,然后将最新值添加到数组中,然后又保存回字典中。

With this structure your dictionary keys stay unique, but instead of holding just one value they hold an array of values. 通过这种结构,您的字典键将保持唯一,但它们不仅包含一个值,还包含一组值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM