简体   繁体   English

UITextField编辑并添加到数组

[英]UITextField editing and adding to array

Okay so i have a series of UITextFields that have the tags between 21 and 35. I am using the UITextField delegate to get the value of the textfield when its finished editing and then adds it to a mutable array. 好吧所以我有一系列UITextFields,其标签介于21和35之间。我使用UITextField委托来获取文本字段的值,当它完成编辑后,然后将其添加到可变数组中。 The code for the delegate method is as follows: 委托方法的代码如下:

- (void)textFieldDidEndEditing:(UITextField *)textField{

if(textField.tag >=21 && textField.tag <36){
    if(textField.text.length != 0)
    [ingredients addObject:textField.text];
  }
}

So when the the text field has finished editing it adds its value into the mutable array. 因此,当文本字段完成编辑时,它将其值添加到可变数组中。 This all works perfectly fine. 一切都很好。 However my problem occurs if that text field is edited again, if the user changes the values in that text field it will then add another value into the array, i would like the previous value to be edited rather than creating a new value in the array. 但是,如果再次编辑该文本字段会出现问题,如果用户更改该文本字段中的值,则会将另一个值添加到数组中,我希望编辑之前的值而不是在数组中创建新值。

Any help would be appreciated please! 任何帮助将不胜感激!

You could change your ingredients object from NSMutableArray to NSMutableDictionary. 您可以将您的配料对象从NSMutableArray更改为NSMutableDictionary。 Example: 例:

- (void)textFieldDidEndEditing:(UITextField *)textField{

    if(textField.tag >=21 && textField.tag <36){
        if(textField.text.length != 0)
            [ingredients setObject:textField.text forKey:[NSNumber numberWithInt:textField.tag]];
    }
}

When you're initialising, add 15 empty strings to your ingredients array. 初始化时,将15个空字符串添加到配料阵列中。 Then when textFieldDidEndEditing: is called, set the string at the appropriate index (textField.tag - 21) instead of using addObject: 然后,当调用textFieldDidEndEditing:时,将字符串设置为适当的索引(textField.tag - 21)而不是使用addObject:

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

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