我在tableview中有动态数量的文本字段,我将每个文本字段放入IB中的自定义单元格并通过nibName加载单元格.
我想在用户输入数据时验证并显示警报,同时在editisDone时我想从用户获取输入值并将其保存到relavent对象.
例如,这些是我可以使用的一些委托方法:
- (void)textFieldDidEndEditing:(UITextField *)textField{
//save the data
}
- (IBAction)textFieldDoneEditing:(id)sender {
//hide the keypad when done is pressed
[sender resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange{}
Run Code Online (Sandbox Code Playgroud)
2个问题:
1 - 当获得用户输入并验证输入时,我将如何知道哪个文本字段的委托被触发,因为有动态数量的单元格和文本字段,我该如何管理它?
2 - 为了隐藏键盘,我这样做但不确定这是否正确; - 在IB中我打开了customcell - >右键单击uitextfield并将其didEndonExit连接到FirstResponder的textFieldDoneEditing方法.这有效,但如果我没有添加任何字符到textfield,我就无法返回.所以它强迫写东西以按下按钮.
关于你的第一个问题......
在下面的代码中,我假设UITextField每个单元格中都有一个.我还假设你已经创建了一个包含一个UITableViewCell子类的子类.CustomCellUITextField
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:CellIdentifier] autorelease];
cell.textField.tag = indexPath.row;
cell.textField.delegate = self;
}
return cell;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"textField tag: %d", textField.tag); // this will show which textField did end editing ...
}
Run Code Online (Sandbox Code Playgroud)
关于你的第二个问题; 如果我正确理解您的问题,取消选中IB中文本字段属性中的"自动启用返回键"应该允许您按下返回按钮,即使它是空的.我在UIView中的一个简单的文本字段中对此进行了测试,但它应该适用于您的情况.

| 归档时间: |
|
| 查看次数: |
6437 次 |
| 最近记录: |