2010-08-04 24 views
5

Estoy tratando de agregar uitextfield a mi alterview. Cuando el usuario intenta ingresar texto, se supone que el alterview se desplaza un poco para que el teclado no se superponga y cuando se presiona la tecla "done" se supone que el teclado debe desaparecer y el alertview debe retroceder.iphone-sdk: ¿Agregar un campo de texto a UIAlertview no funciona en iOS 4?

Todo funciona bien cuando se ejecuta en iOS 3.1.2 (y también en 3.2) pero tan pronto como intento ejecutarlo en iOS 4, el alertview se muestra en la posición incorrecta y el teclado no desaparecerá. ¿Alguna sugerencia? Aquí está mi código:

- (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]; 
    } 
} 


} 

Respuesta

5

También estoy usando alertview con un campo de texto en mi aplicación, & que también en ioS 4.0. Funciona bien.

Aquí es el código de ejemplo: _

-(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]; 
} 

También se puede hacer referencia a los siguientes dos métodos que se llaman las notificaciones de teclado.

-(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

no tengo idea (aún) en relación con el posicionamiento de la alerta, pero es posible que intente enviar resignFirstResponder en su aplicación a la textFieldShouldReturnalertView.

Además, eso debería hacer que el teclado desaparezca (incluso si no sé exactamente por qué).

1

Tuve este problema que es nuevo en iOS 4 también. Traté de hacer una traducción y me di cuenta de que no importa la traducción, solo que se llamaba una traducción.

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

Debe haber una devolución de llamada que es despedido de la traducción, pero esto soluciona el problema. Debería actuar como lo hizo en pre iOS 4.0.

0

Abra el archivo xib, haga clic en UIView y asegúrese de que la casilla de verificación "User Interaction Enabled" esté marcada.

Cuestiones relacionadas