从多个 UITextField 获取值

发布于 2025-01-02 23:35:08 字数 3068 浏览 1 评论 0原文

我有一个分组的静态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2025-01-09 23:35:08

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.

蓝梦月影 2025-01-09 23:35:08

您可以通过 textField.text 获取数组中每个文本字段的值。

you can take values of each textFields in a array by textField.text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文