iPhone:键盘没有隐藏

时间:2011-05-02 12:49:36

标签: ios iphone uitextfield uialertview resignfirstresponder

我有一个情况,我有一个警报视图,其中以编程方式绘制了一个文本字段

UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Enter you name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];

    [nameField setBorderStyle:UITextBorderStyleRoundedRect];
    [nameField setPlaceholder:@"Enter Owners name"];
    [nameField setTextAlignment:UITextAlignmentCenter];
    [nameField setDelegate:self];
    [nameField setBackgroundColor:[UIColor clearColor]];

    [alertView addSubview:nameField];
    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -80.00);
    [alertView setTransform: moveUp];

    [alertView show];
    [alertView release];

隐藏我用过的键盘

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

[textField resignFirstResponder];
return YES;

}

但键盘没有隐藏。请帮助我。

1 个答案:

答案 0 :(得分:4)

您可以使用此隐藏键盘。单击模拟器/模拟器/ iOS设备的背景时,这将起作用。

  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

            [yourtextfield resignFirstResponder];

            // you can have multiple textfields here


        }