2013-02-02 2 views
0

Je souhaite détecter la modification de certains UItextField et UItextView afin de lancer une animation. Je me suis approché le problème de cette façon:Masquer et afficher le clavier IOS lors de l'édition de UItextview

-(void)textFieldDidBeginEditing: (UITextField *)textField{ 
NSLog(@"sowing keyboard"); 

if ((textField == timebegin)||(textField == timeend)||(textField == number)||(textField == costlist)||(textField == costnormal)||(textField == newmessagename)||(textField == noteView))  { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
}else{ 

} 
} 

-(void)textFieldDidEndEditing: (UITextField *)textField{ 
NSLog(@"hiding keyboard"); 

} 

-(void)textViewDidBeginEditing: (UITextView *)textView{ 
NSLog(@"sowing keyboard"); 

if (textView == generalDetails) { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
}else{ 

} 
} 

-(void)textViewDidEndEditing: (UITextView *)textView{ 
NSLog(@"hiding keyboard"); 

} 

- (void)keyboardDidHide:(NSNotification *)aNotification { 

[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardDidHideNotification object:nil]; 

[[NSNotificationCenter defaultCenter] removeObserver:self name: UIKeyboardWillShowNotification object:nil]; 

NSLog(@"hiding keyboard"); 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.27]; 
scroll1.frame = CGRectMake(0, -340, 768, 1004); 
[UIView commitAnimations]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.37]; 
scroll2.frame = CGRectMake(0, 678, 768, 1004); 
[UIView commitAnimations]; 

[self.mapView setHidden:NO]; 
    addImageForCommentingButton.hidden = NO; 

//fade in 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationDelegate:self]; 

[self.mapView setAlpha:1.0]; 
    [addImageForCommentingButton setAlpha: 1.0]; 

[UIView commitAnimations]; 

} 

- (void)keyboardWillShow:(NSNotification *)notification { 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.27]; 
scroll1.frame = CGRectMake(0, -604, 768, 1004); 
[UIView commitAnimations]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.47]; 
scroll2.frame = CGRectMake(0, 414, 768, 1004); 
[UIView commitAnimations]; 



[UIView animateWithDuration:1.0 
      animations:^{ 
       self.mapView.alpha=0.0; 
       [addImageForCommentingButton setAlpha: 0.0]; 
      } 
      completion:^(BOOL finished){ 
       self.mapView.hidden=YES; 
       addImageForCommentingButton.hidden = YES; 
      }]; 
} 

Quand je commence à l'édition de l'un des UITextFields dont le nom est dans l'instruction if, l'observateur est ajouté et l'animation dans la méthode showKeyboard court. ce n'est pas la même chose pour les détails généraux d'UItextView. quand je commence à l'éditer, l'animation ne s'exécute pas, comme si le code n'était pas appelé, mais il est appelé, et l'observateur est ajouté (j'ai compris cela à partir de NSlog). Le point est que c'est le même code pour UItextView et UItextField, alors comment peut-on travailler et l'autre pas?

+0

Si tout ce que vous avez besoin est d'appeler une méthode sur la modification d'un champ, pourquoi ne pas vous inscrire aux notifications de clavier OS? – Stavash

+0

car, je veux que les animations se produisent uniquement lorsque certains champs sont édités, pas tous – user2014474

Répondre

0

Résolu en faisant ceci:

-(void)textViewDidBeginEditing: (UITextView *)textView{ 
NSLog(@"sowing keyboard textView"); 

if (textView == generalDetails) { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

    [self keyboardWillShow]; 

}else{ 

} 
} 

très étrange, je pense que la méthode keyboardWillShow n'a pas été appelé à tous

0

Si vous ajoutez le [self keyboardWillShow] a résolu le problème, alors les chances sont votre méthode n'a pas été mis en œuvre correctement. keyboardWillShow et keyboardWillShow: sont deux méthodes différentes.

Si vous voulez une notification keyboardWillShow: à appeler alors il doit être défini comme suit:

- (void) keyboardWillShow: (NSNotification*) notification 
{ 
} 
Questions connexes