我正在尝试添加 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];
    }
}


}
有帮助吗?

解决方案

我也使用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];

}
}

其他提示

我有这个问题,即在IOS 4是新为好。我试图做的译本,并意识到这不要紧的翻译,只是翻译被调用。

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

有必须是被从翻译发射的回调,但是这能解决问题。因为它在前期的iOS 4.0的确应该采取行动。

这是 UIAlertView 与 UITextField 结合的更清晰的实现: DDAlert提示 或者 EGO文本字段警报视图.

我有一个关于该警报的定位没有任何线索(还),但你可以尝试在你的resignFirstResponder落实到textFieldShouldReturn发送alertView

此外,应使键盘消失(即使我完全不知道为什么)。

打开xib文件,点击UIView,确保了“User Interaction Enabled”复选框被选中。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top