问题
I am trying to add uitextfield
to my alterview
. When the user tries to enter text the alterview
is supposed to shift up a little bit so the keyboard does not overlap and when pressing the done key the keyboard is supposed to disappear and the alertview
should shift back.
It all works fine when run it in iOS 3.1.2 (and also in 3.2) but as soon as I try to run it under iOS 4 the alertview
is displayed in the wrong position and the keyboard won't disappear. Any suggestions? Here is my code:
- (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];
}
}
}
回答1:
I am also using alertview with a textfield in my application, & that too on ioS 4.0. Its working fine.
Here is the sample code:_
-(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];
}
Also you can refer the following two methods which are called on Keyboard notifications.
-(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];
}
}
回答2:
I had this problem that is new in iOS 4 as well. I tried to do a translation on it and realized that it doesn't matter the translation, only that a translation was called.
myAlert = my AlertView
CGAffineTransform rotate = CGAffineTransformMakeTranslation(0.0f, 0.0f);
myAlert.transform = rotate;
[myAlert show];
[myAlert release];
There must be a callback that gets fired from the translation but this fixes the problem. It should act as it did in pre-iOS 4.0.
回答3:
Here is a cleaner implementation of a UIAlertView coupled with a UITextField: DDAlertPrompt or EGOTextFieldAlertView.
回答4:
I have no clue (yet) regarding the positioning of the alert, but you might try sending resignFirstResponder
in your textFieldShouldReturn
implementation to the alertView
.
Also, that should make the keyboard disappear (even if I do not know exactly why).
回答5:
Open the xib
file, click on the UIView
and make sure that the "User Interaction Enabled
" check box is ticked.
来源:https://stackoverflow.com/questions/3410372/iphone-sdk-adding-a-textfield-to-uialertview-does-not-work-in-ios-4