iPhone-SDK:在iOS 4中向UIAlertview添加文本框无法工作?

5
我正在尝试将uitextfield添加到我的alterview中。当用户尝试输入文本时,alterview应该向上移动一点,以免键盘重叠,并且在按下完成键时,键盘应该消失,alertview应该恢复原位。
当我在iOS 3.1.2(和3.2)中运行它时,所有功能都正常工作,但是一旦我尝试在iOS 4下运行它,alertview显示在错误的位置,键盘无法消失。有什么建议吗?这是我的代码:
- (void)addItemAction{

workoutName = [[UIAlertView alloc] initWithTitle:@"New Workout" message:@"Insert the name of your new workout:\n                " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
workoutName.cancelButtonIndex = 0;
UITextField *titleField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)];
titleField.delegate = self;
titleField.borderStyle = UITextBorderStyleRoundedRect;
titleField.returnKeyType = UIReturnKeyDone;
[workoutName addSubview:titleField];
[workoutName show];


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


[textField resignFirstResponder];
return YES;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField {

[UIView beginAnimations:nil context:NULL];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -70.0);
[workoutName setTransform:myTransform];
[UIView commitAnimations];

}

- (void)textFieldDidEndEditing:(UITextField *)textField {

[UIView beginAnimations:nil context:NULL];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 0.0);
[workoutName setTransform:myTransform];
[UIView commitAnimations];
self.newWorkout = textField.text;

}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

if (buttonIndex == 1) {
    if (self.newWorkout != @"TestWorkout"){
    [self.workoutPlanArray insertObject:[NSDictionary dictionaryWithObjectsAndKeys:self.newWorkout, @"titleValue", @"04.08.10", @"dateValue", nil] atIndex:counter];
    counter++;
    [self.tableView reloadData];
    }
}


}
5个回答

5

我在我的应用程序中还使用了带有文本字段的alertview,而且是在iOS 4.0上。 它正常工作。

这里是示例代码:_

-(void)showAlert{

showAlert=YES; //boolean variable

createNewAlert =[[UIAlertView alloc] initWithTitle:@"ADD item" message:@"Put it blank textfield will cover this" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

UITextField *txtName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
txtName.text=@"";
[txtName setBackgroundColor:[UIColor whiteColor]];
[txtName setKeyboardAppearance:UIKeyboardAppearanceAlert];
[txtName setAutocorrectionType:UITextAutocorrectionTypeNo];

[txtName setTextAlignment:UITextAlignmentCenter];
[createNewAlert addSubview:txtName];


[createNewAlert show];
}

您可以参考以下两种方法,它们被称为键盘通知。
-(void)keyboardWillShow: (id) notification {

if(showAlert==YES)
{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)];
    [createNewAlert show];
    [UIView commitAnimations];
}

}

-(void)keyboardWillHide: (id) notification {

if(showAlert==YES) 
{


        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)];

        [UIView commitAnimations];

}
}

1

我也遇到了这个问题,它在iOS 4中是新的。我试图对其进行翻译,但意译并不重要,只要调用了翻译就可以。

myAlert = my AlertView
CGAffineTransform rotate = CGAffineTransformMakeTranslation(0.0f, 0.0f);
myAlert.transform = rotate;
[myAlert show];
[myAlert release];

翻译过程中必须触发回调函数,但这可以解决问题。它应该像iOS 4.0之前那样运作。


0

0

我还不清楚警告的位置,但你可以尝试在textFieldShouldReturn实现中发送resignFirstResponderalertView

此外,这应该会使键盘消失(即使我不知道确切原因)。


0
打开 xib 文件,点击 UIView 并确保 "User Interaction Enabled" 复选框被勾选。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接