简体   繁体   English

单击按钮时如何获取 UITextField 值

[英]How to get UITextField values when button is clicked

I have this view:我有这样的看法:

在此处输入图像描述

and these implementations:以及这些实现:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableLogin dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if ([indexPath section] == 0) {
            UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 165, 30)];
            textfield.adjustsFontSizeToFitWidth = YES;
            textfield.textColor = [UIColor blackColor];
            textfield.backgroundColor = [UIColor whiteColor];
            textfield.autocorrectionType = UITextAutocorrectionTypeNo;
            textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
            textfield.textAlignment = UITextAlignmentLeft;
            textfield.clearButtonMode = UITextFieldViewModeNever;
            textfield.delegate = self;

            if ([indexPath row] == 0) {
                textfield.tag = 0;
                textfield.placeholder = @"your username";
                textfield.keyboardType = UIKeyboardTypeDefault;
                textfield.returnKeyType = UIReturnKeyNext;
            }
            else {
                textfield.tag = 1;
                textfield.placeholder = @"required";
                textfield.keyboardType = UIKeyboardTypeDefault;
                textfield.returnKeyType = UIReturnKeyDone;
                textfield.secureTextEntry = YES;
            }

            [textfield setEnabled:YES];
            [cell addSubview:textfield];
            [textfield release];
        }
    }
    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            cell.textLabel.text = @"Email";
        }
        else {
            cell.textLabel.text = @"Password";
        }
    }

    return cell;    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            self.username = textField.text;
            break;
        case 1:
            self.password = textField.text;
            break;
    }
    return true;
}

When the user click Login button without tap "Done" for close the keyboard it doesn't save the field values.当用户单击登录按钮而不点击“完成”以关闭键盘时,它不会保存字段值。 How can I tricky-fix this?我该如何解决这个问题?

Two options:两种选择:

Keep a reference to both textfields and pull their text values in the "Done" button's action method.保留对两个文本字段的引用并在“完成”按钮的操作方法中提取它们的text值。

Alternatively, register for the UITextFieldTextDidChangeNotification and capture input as it happens.或者,注册UITextFieldTextDidChangeNotification并在输入发生时捕获输入。

You can use - (void)textFieldDidEndEditing:(UITextField *)textField and save your values there, in addition to hitting the return key.您可以使用- (void)textFieldDidEndEditing:(UITextField *)textField并将您的值保存在那里,除了点击返回键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM