【问题标题】:How do I identify a UITextFiled apart from tags除了标签,我如何识别 UITextFiled
【发布时间】:2017-05-13 19:21:17
【问题描述】:

经历了类似的问题,tags 基本上是所有问题的答案。问题是我有一个自定义的UITableViewCell,它有两个文本字段名和姓。在我的应用程序中,我有一个 + 按钮,当单击该按钮时,将新行添加到表视图并重新加载表视图。现在早些时候,如果用户键入内容然后单击+ 按钮,则会添加一个新行,但第一行中的名字和姓氏会消失。为了解决这个问题,我使用了NSMutableArray,比如fNameArray,并添加了用户在- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason 中输入的内容,这很好用,但现在我必须为姓氏创建另一个NSMutableArray,问题是我没有知道我将如何识别上述委托中的文本字段。目前我将cellForRowAtIndexPath中的标签设置为cell.tf_firstName.tag = indexPath.row;

【问题讨论】:

    标签: ios objective-c uitableview uitextfield


    【解决方案1】:

    如果将相同的标记值分配给多个文本字段并且对所有文本字段使用相同的委托,则tag 属性将不起作用。

    以下是通过为每组文本字段使用不同委托来解决此问题的实现。

    TextFieldArrayManager 管理一组文本字段及其数据。它充当其管理的文本字段的委托。

    @interface TextFieldArrayManager : NSObject <UITextFieldDelegate>
    @property NSMutableArray *textItems;
    @end
    
    @implementation TextFieldArrayManager
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        if (_textItems.count >= textField.tag + 1) {
            if (textField.text) {
                _textItems[textField.tag] = textField.text;
            }
            else {
                _textItems[textField.tag] = @"";
            }
        }
    }
    @end
    

    视图控制器使用单独的TextFieldArrayManager 来管理名字和姓氏。

    @interface ObjcTableViewController ()
    @end
    
    @implementation ObjcTableViewController
    
    TextFieldArrayManager *firstNames;
    TextFieldArrayManager *lastNames;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        firstNames = [[TextFieldArrayManager alloc] init];
        firstNames.textItems = [NSMutableArray arrayWithObjects:@"George", @"Ludwig", @"Wolfgang", nil];
    
        lastNames = [[TextFieldArrayManager alloc] init];
        lastNames.textItems = [NSMutableArray arrayWithObjects:@"Handel", @"Beethoven", @"Mozart", nil];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return firstNames.textItems.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ObjcTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        cell.firstName.delegate = firstNames;
        cell.firstName.text = firstNames.textItems[indexPath.row];
        cell.firstName.tag = indexPath.row;
    
        cell.lastName.delegate = lastNames;
        cell.lastName.text = lastNames.textItems[indexPath.row];
        cell.lastName.tag = indexPath.row;
    
        return cell;
    }
    

    要向表中添加一个新的空行,您可以这样做:

    [firstNames.textItems addObject:@""];
    [lastNames.textItems addObject:@""];
    [self.tableView reloadData];
    

    当用户输入文字时,会保存到textItems中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2021-12-21
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多