从几个UITextField获取值

时间:2012-02-06 06:14:40

标签: iphone objective-c xcode ipad

我有一个分组的静态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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

1)您可以创建这些字段的属性..然后通过属性name.text

获取文本

2)您可以尝试为字段提供标记,然后使用viewwithtag方法获取这些字段。然后他们的文字。