调整alertview中存在的文本字段的键盘

时间:2013-03-27 05:16:17

标签: iphone keyboard uitextfield

我在alertview上有三个文本字段,我将键盘设置为decimalType,

- (IBAction)heightMethod:(id)sender

{

 self.utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = @"  Centimeters";

self.utextfield.delegate=self;

self.utextfield.tag=3;
[ self.utextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.utextfield];
// Adds a password Field
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = @"   Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;

[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];

self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = @"   Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;


[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];

 [self.utextfield setKeyboardType:UIKeyboardTypeDecimalPad];
 [self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
 [self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];

[self.alertHeight show];

}

当我点击任何文本字段时,键盘只会辞职两次,但第三次它不会辞职。我在alertview的delgate方法中添加了resignfirst responder方法,请看这里

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

3 个答案:

答案 0 :(得分:1)

创建UITextField的iVar,并在方UITextFieldDelegate方法textFieldShouldBeginEditing中分配。希望它应该有用。

如下所示:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   textField = iVar;
}

答案 1 :(得分:0)

尝试

- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.view endEditing: YES];
}

答案 2 :(得分:0)

当您在警报视图上按OK按钮时,键盘会自动关闭。我已经测试过,所以从上述方法中删除 resignfirstresponder 并尝试