2010-08-04 72 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」複選框。