键盘不会消失

时间:2011-06-05 09:07:51

标签: iphone objective-c ipad

我以编程方式将uitextfield添加到uitableview单元格,问题是当我在文本字段上键入并单击“完成”或从文本字段移动时键盘不会消失。

有什么建议可以解决这个问题吗?

@interface test_20110605ViewController : UIViewController <UITextFieldDelegate> {

    UITextField *PtienttextField ;
}

@property ( nonatomic , retain ) UITextField *PtienttextField ;

@end

at m file 

@implementation test_20110605ViewController

@synthesize PtienttextField ;


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

        if (indexPath.row == 0 && indexPath.section == 1 ) {


            //cell.textLabel.text = "Patient Name";



            UILabel *PatientLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 25)];
            PatientLabel.backgroundColor = [UIColor clearColor];
            PatientLabel.tag = 1;
            PatientLabel.text = @"Patient Name" ; 
            [cell.contentView addSubview:PatientLabel];

            PtienttextField = [[UITextField alloc]initWithFrame:CGRectMake(140, 10, 400, 30)];

            PtienttextField.clearsOnBeginEditing = YES;
            PtienttextField.textAlignment = UITextAlignmentRight;
            PtienttextField.returnKeyType = UIReturnKeyDone;
            PtienttextField.delegate = self;
            //PtienttextField.tag = 2 ; 
            [PtienttextField setBorderStyle:UITextBorderStyleBezel];
            [cell.contentView addSubview:PtienttextField];
        }
    }

    // Configure the cell.

    return cell;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{


    return YES;
}

@end

2 个答案:

答案 0 :(得分:3)

我认为你在textFieldShouldReturn中缺少以下内容:

[textField resignFirstResponder];

然后是您的return YES;

答案 1 :(得分:0)

尝试在键盘出现时进行NSNotification,并将textfield设置为firstresponder。您使用的问题可能是因为textfield不是您案例中的第一个响应者。