从多个 UITextField 获取值
我有一个分组的静态 UITableViewController,每个部分 3 行。每行包含一个 UITextField。如何获取每个 UITextField 的值?
到目前为止,我所看到的是如何获取一个 UITextField 的值。如果我必须获得多个,我该怎么办?
我计划将这些字段的值放入 NSMutableDictionary 中。
这是我制作表格视图的代码:(省略无关部分)
#import "AddItemTableViewController.h"
#import "iMonggoFetcher.h"
@interface AddItemTableViewController() <UITextFieldDelegate>
@property (weak, nonatomic) NSMutableDictionary *productItem;
//required
@property (weak, nonatomic) IBOutlet UITextField *stockNoTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *retailPriceTextField;
//basic info
@property (weak, nonatomic) IBOutlet UITextField *costTextField;
@property (weak, nonatomic) IBOutlet UITextField *descriptionTextField;
@property (weak, nonatomic) IBOutlet UITextField *barcodesTextField;
@property (weak, nonatomic) IBOutlet UITextField *tagsTextField;
@property (weak, nonatomic) IBOutlet UISwitch *exemptFromTaxSwitch;
//advanced features
@property (weak, nonatomic) IBOutlet UISwitch *allowDecimalQuantitiesSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *enableOpenPriceSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *disableDiscountSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *disableInventorySwitch;
@end
@implementation AddItemTableViewController
@synthesize productItem = _productItem;
@synthesize stockNoTextField = _stockNoTextField;
@synthesize nameTextField = _nameTextField;
@synthesize retailPriceTextField = _retailPriceTextField;
@synthesize costTextField = _costTextField;
@synthesize descriptionTextField = _descriptionTextField;
@synthesize barcodesTextField = _barcodesTextField;
@synthesize tagsTextField = _tagsTextField;
@synthesize exemptFromTaxSwitch = _exemptFromTaxSwitch;
@synthesize allowDecimalQuantitiesSwitch = _allowDecimalQuantitiesSwitch;
@synthesize enableOpenPriceSwitch = _enableOpenPriceSwitch;
@synthesize disableDiscountSwitch = _disableDiscountSwitch;
@synthesize disableInventorySwitch = _disableInventorySwitch;
- (IBAction)save:(UIBarButtonItem *)sender {
[self.productItem setValue:self.stockNoTextField.text forKey:IMONGGO_PRODUCT_STOCK_NO];
[self.productItem setValue:self.nameTextField.text forKey:IMONGGO_PRODUCT_NAME];
[self.productItem setValue:self.retailPriceTextField.text forKey:IMONGGO_PRODUCT_RETAIL_PRICE];
}
#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// optionally trigger delegate method here
}
#pragma mark - View lifecycle
- (void) viewWillAppear:(BOOL)animated{
//[self.stockNoTextField becomeFirstResponder];
}
- (void) viewDidLoad{
[super viewDidLoad];
self.stockNoTextField.delegate = self;
self.nameTextField.delegate = self;
self.retailPriceTextField.delegate = self;
//will do the rest later, just trying it out for now
}
@end
I have a grouped static UITableViewController, with 3 rows per section. Each row contains a UITextField. How do I get the value for each UITextField?
So far, what I've seen is how to get the value of one UITextField. What should I do if I have to get more than one.
I plan to put the values of these fields in a NSMutableDictionary.
Here's my code for making the table view: (irrelevant parts are omitted)
#import "AddItemTableViewController.h"
#import "iMonggoFetcher.h"
@interface AddItemTableViewController() <UITextFieldDelegate>
@property (weak, nonatomic) NSMutableDictionary *productItem;
//required
@property (weak, nonatomic) IBOutlet UITextField *stockNoTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *retailPriceTextField;
//basic info
@property (weak, nonatomic) IBOutlet UITextField *costTextField;
@property (weak, nonatomic) IBOutlet UITextField *descriptionTextField;
@property (weak, nonatomic) IBOutlet UITextField *barcodesTextField;
@property (weak, nonatomic) IBOutlet UITextField *tagsTextField;
@property (weak, nonatomic) IBOutlet UISwitch *exemptFromTaxSwitch;
//advanced features
@property (weak, nonatomic) IBOutlet UISwitch *allowDecimalQuantitiesSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *enableOpenPriceSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *disableDiscountSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *disableInventorySwitch;
@end
@implementation AddItemTableViewController
@synthesize productItem = _productItem;
@synthesize stockNoTextField = _stockNoTextField;
@synthesize nameTextField = _nameTextField;
@synthesize retailPriceTextField = _retailPriceTextField;
@synthesize costTextField = _costTextField;
@synthesize descriptionTextField = _descriptionTextField;
@synthesize barcodesTextField = _barcodesTextField;
@synthesize tagsTextField = _tagsTextField;
@synthesize exemptFromTaxSwitch = _exemptFromTaxSwitch;
@synthesize allowDecimalQuantitiesSwitch = _allowDecimalQuantitiesSwitch;
@synthesize enableOpenPriceSwitch = _enableOpenPriceSwitch;
@synthesize disableDiscountSwitch = _disableDiscountSwitch;
@synthesize disableInventorySwitch = _disableInventorySwitch;
- (IBAction)save:(UIBarButtonItem *)sender {
[self.productItem setValue:self.stockNoTextField.text forKey:IMONGGO_PRODUCT_STOCK_NO];
[self.productItem setValue:self.nameTextField.text forKey:IMONGGO_PRODUCT_NAME];
[self.productItem setValue:self.retailPriceTextField.text forKey:IMONGGO_PRODUCT_RETAIL_PRICE];
}
#pragma mark - UITextField
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// optionally trigger delegate method here
}
#pragma mark - View lifecycle
- (void) viewWillAppear:(BOOL)animated{
//[self.stockNoTextField becomeFirstResponder];
}
- (void) viewDidLoad{
[super viewDidLoad];
self.stockNoTextField.delegate = self;
self.nameTextField.delegate = self;
self.retailPriceTextField.delegate = self;
//will do the rest later, just trying it out for now
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)您可以为这些字段创建属性..然后通过属性名称获取文本。文本
2)您可以尝试为字段提供标签,然后使用 viewwithtag 方法获取这些字段。然后是他们的文字。
1) You can make properties of these fields.. and then get the text by the property name.text
2) You can try giving the fields a tag and then get these fields using viewwithtag method. and then their text.
您可以通过 textField.text 获取数组中每个文本字段的值。
you can take values of each textFields in a array by textField.text.