UITableViewCell中的UITextField和模式视图中的验证 [英] UITextField in UITableViewCell and validation in modal view

查看:82
本文介绍了UITableViewCell中的UITextField和模式视图中的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此stackoverflow发布中描述的方法来检索值来自文本字段。我的问题是表格视图是以模态显示的,并且我有一个保存按钮,用于验证输入并将其存储。



问题在于,当用户单击 UIBarButtonItem 时,不会不调用 textFieldDidEndEditing 方法(=保存按钮,它会关闭模式视图。)



在这种情况下(当用户要保存输入时),我想对其进行验证。但是这些值存储在 textFieldDidEndEditing 的属性中。由于未调用此方法,因此我无法正确验证输入值。



有人对此有任何提示或解决方案吗?



谢谢!

解决方案

好的,我们开始:



感谢@Lefteris及其存储当前索引的想法。由于无法将索引存储到 tag 属性中,因此我决定存储活动的 indexPath 以及有效的 textField 。 (我知道,对 UITextField 的引用就足够了,但是我需要其他内容)



第一个我添加了这两个属性:

  @property(非原子的,强的)NSIndexPath * activeIndexPath; 
属性(非原子的,强的)UITextField * activeTextField;

然后我实现了 textFieldDidBeginEditing: textFieldDidEndEditing: of UITextFieldDelegate

 -(void)textFieldDidBeginEditing:(UITextField *)textField 
{
NSIndexPath * indexPath =(NSIndexPath *)[self.tableView indexPathForCell:(UITableViewCell *)[[textField superview] superview]];

self.activeTextField = textField;
self.activeIndexPath = indexPath;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSString * input = textField.text;

//将输入文本字段中的值假定为相应的属性
[self承担Input:input withIndexPath:self.activeIndexPath];

self.activeTextField = nil;
self.activeTextField = nil;
}

textFieldDidEndEditing中:我将值存储到属性中(例如 self.firstName self.lastName ,等等。) 。)使用方法 [self承担Input:input withIndexPath:self.activeIndexPath];



在我的 saveAction -方法是从当前活动的 TextField 中存储值。

 -(IBAction)saveButtonClicked:(UIBarButtonItem *)sender 
{
//假设来自活动字段的输入(didEndEditing _not_现在被调用!)
[self承担Input:self.activeTextField.text withIndexPath:self.activeIndexPath];

//测试输出
NSLog(@ firstName:%@,self.firstName);
NSLog(@ lastName:%@,self.lastName);
NSLog(@电子邮件:%@,self.email);
...
}

...就是这样!

希望有帮助!感谢@Lefteris的投入。



最好,
Chris


I am using the approach described in this stackoverflow posting to retrieve values from a textfield. My problem is that the tableview is presented modally and I have a save button that validates the input and stores it.

The problem is that the textFieldDidEndEditing method is not called when the user clicks an UIBarButtonItem (= the save button, which closes the modal view).

In this event (when the user wants to save the input) I would like to validate it. But the values are stored in properties in the textFieldDidEndEditing. Due to the fact that this method is not called, I cannot validate the input values correctly.

Does anyone have a hint or solution on this?

Thanks in advance!

解决方案

Okay, here we go:

Thanks to @Lefteris and his idea with storing the current index. Due to the fact that I cannot store the index into the tag attribute I decided to store the active indexPath and additionally the active textField. (I know, a reference to the UITextField would have been enough but I needed it for other stuff)

First I have added these two properties:

@property (nonatomic, strong) NSIndexPath *activeIndexPath;
@property (nonatomic, strong) UITextField *activeTextField;

Then I implemented textFieldDidBeginEditing: and textFieldDidEndEditing: of UITextFieldDelegate.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSIndexPath *indexPath = (NSIndexPath*)[self.tableView indexPathForCell:(UITableViewCell*)[[textField superview] superview]];

    self.activeTextField = textField;
    self.activeIndexPath = indexPath;
}

- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    NSString *input = textField.text;    

    //assuming values from input textfield into corresponding properties
    [self assumeInput:input withIndexPath:self.activeIndexPath];

    self.activeTextField = nil;
    self.activeTextField = nil;
}

In textFieldDidEndEditing: I am storing the values into my properties (such as self.firstName, self.lastName, and so on...) by using the method [self assumeInput:input withIndexPath:self.activeIndexPath];.

In my saveAction-Method I am storing the value from the currently active TextField.

- (IBAction)saveButtonClicked:(UIBarButtonItem *)sender 
{    
    //assuming input from active field (didEndEditing _not_ called right now!)
    [self assumeInput:self.activeTextField.text withIndexPath:self.activeIndexPath];

    //test output
    NSLog(@"firstName: %@", self.firstName);
    NSLog(@"lastName: %@", self.lastName);
    NSLog(@"email: %@", self.email);
    ...
}

... and that's it!

Hope it helps! Thanks to @Lefteris for his input.

Best, Chris

这篇关于UITableViewCell中的UITextField和模式视图中的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆