简体   繁体   中英

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. But i want to store all the data provided in those 5 pairs of textfield. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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