Pregunta

Estoy tratando de añadir a mi uitextfield alterview. Cuando el usuario intenta introducir texto del alterview se supone que se desplazan hacia arriba un poco por lo que el teclado no se solapa y al pulsar la tecla Terminar el teclado se supone que debe desaparecer y el alertview debe cambiar de nuevo.

Todo funciona bien cuando se ejecuta en iOS 3.1.2 (y también en 3.2) pero tan pronto como trato de ejecutar el programa bajo el IOS 4 alertview se muestra en la posición incorrecta y el teclado no va a 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];
    }
}


}
¿Fue útil?

Solución

También estoy usando alertview con un campo de texto en mi solicitud, y que también en iOS 4.0.  Su funcionamiento muy bien.

Aquí está 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)];
txtName.text=@"";
[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];

}
}

Otros consejos

Yo tenía este problema que es nuevo en iOS 4 también. Traté de hacer una traducción en él y se dio cuenta de que no importa la traducción, sólo que una traducción se llama.

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

No debe ser una devolución de llamada que es despedido de la traducción, pero esto soluciona el problema. Debe actuar como lo hizo en pre-IOS 4.0.

Esta es una implementación de un limpiador de UIAlertView junto con un UITextField: DDAlertPrompt o EGOTextFieldAlertView .

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 textFieldShouldReturn alertView.

Además, eso debería hacer desaparecer el teclado (aunque no sé exactamente por qué).

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top