2012-02-06 32 views
2

我有一個分組的靜態UITableViewController,每節有3行。每行包含一個UITextField。我如何獲得每個UITextField的值?從幾個UITextFields獲取值

到目前爲止,我所看到的是如何獲得一個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 
+1

向我們展示您的代碼,用於製作UITableViewCell .. – 2012-02-06 06:17:01

+0

請參閱上面的@AdilSoomro編輯。 – acecapades 2012-02-06 06:29:30

+0

告訴我們您的cellForRowAtIndexPath代碼: – Ankur 2012-02-06 10:43:59

回答

0

1)您可以通過這些領域的性質..然後得到由name.text

2)你可以嘗試給字段標籤,然後使用viewwithtag方法獲得這些字段屬性的文本。然後他們的文字。

+0

謝謝@shubhank我如何添加標籤? – acecapades 2012-02-06 06:36:48

+0

當你創建一個文本字段..然後添加一行Yourtextfiel.tag =一些整數..稍後在代碼中,當你需要值。你可以做UitextField * a =(UitextField *)(self.view getviewwithtag:一些整數)。在當前代碼中存在拼寫錯誤..不記得確切的語法..所以使用自動完成) – Shubhank 2012-02-06 06:40:33

+0

如果你需要保存文本字段的所有值。使所有文本字段標記按照升序從1到6 ..或8 ..等等。然後做一個for循環。對於我= 0和我<最大標記..i ++然後獲取當前我的文本字段,然後將該值添加到字典 – Shubhank 2012-02-06 06:42:41

0

可以採取各TextField的值由一個的TextField.text陣列英寸

+0

是UITextField的屬性? – acecapades 2012-02-06 06:37:07