2010-08-04 40 views
5

我正在尝试将uitextfield添加到我的alterview。当用户尝试输入文本时,alterview应该稍微向上移动一点,这样键盘不会重叠,按下完成键后,键盘应该消失,alertview应该移回。iphone-sdk:将文本框添加到UIAlertview不适用于iOS 4?

在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

我也在我的应用程序中使用了带有textfield的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)]; 
[email protected]""; 
[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]; 

} 
} 
0

我不知道(还),但你可以尝试在你的textFieldShouldReturn实现发送resignFirstResponderalertView

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

1

我有这个问题,在iOS 4中也是新的。我试着翻译它,并意识到翻译并不重要,只是翻译被称为。

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

必须有一个从翻译中被触发的回调,但是这可以解决问题。它应该像在iOS 4.0之前一样。

0

打开xib文件,单击UIView并确保勾选了“User Interaction Enabled”复选框。