2010-08-04 12 views
5

Sto cercando di aggiungere uitextfield al mio alterview. Quando l'utente tenta di immettere del testo, è necessario che lo alterview si sposti un po 'in modo che la tastiera non si sovrapponga e quando si preme il tasto Fine la tastiera dovrebbe scomparire e lo alertview dovrebbe tornare indietro.iphone-sdk: l'aggiunta di un campo di testo a UIAlertview non funziona in iOS 4?

Funziona tutto bene quando viene eseguito in iOS 3.1.2 (e anche in 3.2) ma non appena provo a eseguirlo su iOS 4 lo alertview viene visualizzato nella posizione errata e la tastiera non scompare. Eventuali suggerimenti? Qui è il mio codice:

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


} 

risposta

5

Sto utilizzando anche alertview con un campo di testo nella mia applicazione, & anche su ioS 4.0. È funzionante.

Ecco il codice di esempio: _

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

Inoltre si può fare riferimento i seguenti due metodi che sono chiamati sulle notifiche della tastiera.

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

non ho idea (ancora) per quanto riguarda il posizionamento della segnalazione, ma si potrebbe provare a inviare resignFirstResponder nel vostro textFieldShouldReturn implementazione al alertView.

Inoltre, questo dovrebbe far scomparire la tastiera (anche se non so esattamente perché).

1

Ho avuto questo problema che è nuovo anche in iOS 4. Ho provato a fare una traduzione e ho capito che non importa la traduzione, solo che è stata chiamata una traduzione.

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

Ci deve essere un callback che viene generato dalla traduzione ma questo risolve il problema. Dovrebbe funzionare come in pre-iOS 4.0.

0

Aprire il file xib, fare clic su UIView e assicurarsi che la casella di controllo "User Interaction Enabled" sia spuntata.

Problemi correlati