iphone - 单击按钮时如何获取 UITextField 值

标签 iphone ios uitableview uitextfield

我的看法是:

enter image description here

和这些实现:

- (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;
}

当用户单击“登录”按钮而没有点击“完成”以关闭键盘时,它不会保存字段值。我该如何巧妙地解决这个问题?

最佳答案

两种选择:

保留对两个文本字段的引用,并在“完成”按钮的操作方法中提取它们的 text 值。

或者,注册 UITextFieldTextDidChangeNotification 并在输入发生时捕获它。

关于iphone - 单击按钮时如何获取 UITextField 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7034433/

相关文章:

iphone - 检查更新版本

ios - 无法让 xcode Storyboard 模式按钮更改屏幕

ios - 在tableview中异步设置图像

iphone - 如何检测 UITableView 上的 "quick touch"以便切换到全屏或从全屏切换?

ios - 我的表格 View 在滚动时重用选定的单元格——在 SWIFT 中

iphone - 创建不带应用名称的本地通知

iphone - iPhone 能否确定您面朝北、南、东还是西?

ios - iOS联系人备份我们需要保存的文件扩展名

ios - UITableView 中的 UILabel 被覆盖

swift - 使用 firebase 填充 tableview 时,视频源的每个视频都与最近上传的视频相同