UITextFieldDelegate in iphone sdk


- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillShow:) 
              name:UIKeyboardWillShowNotification object:self.view.window];
 [[NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(keyboardWillHide:) 
              name:UIKeyboardWillHideNotification object:self.view.window];

}
- (void)viewWillDisappear:(BOOL)animated{

 [self setEditing:NO animated:YES];
 // unregister for keyboard notifications while not visible.

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

#pragma mark - UIViewController delegate methods

- (void) keyboardWillShow:(NSNotification *)notif{
    // The keyboard will be shown. If the user is editing the comments, adjust the display so that the
    // comments field will not be covered by the keyboard.

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.3];
 CGRect rect = self.view.frame;
 rect.origin.y = self.view.frame.origin.y - 200;
 self.view.frame = rect;
 [UIView commitAnimations];


}

- (void) keyboardWillHide:(NSNotification *)notif{
    // The keyboard will be hidden.
    // view should be shifted donw to its real position.

 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationDuration:0.3];
 CGRect rect = self.view.frame;
 rect.origin.y = self.view.frame.origin.y + 200;
 self.view.frame = rect;
 [UIView commitAnimations];
 
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
 [textField resignFirstResponder];
 return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {        
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    return !([newString length] >124);


 /*
 if(textField.tag==1)
 {
  //return integer
  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
  [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
  NSNumber* candidateNumber;
  NSString* candidateString = [amendment.text stringByReplacingCharactersInRange:range withString:string];
  range = NSMakeRange(0, [candidateString length]);
  [numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];
 
  if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {
  
   return NO;
  }
  else 
  {
   return YES;
  }
 
 
 
 }
 else
 {
  NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  return !([newString length] >500);
 }
 */

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
 NSString *newString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    return !([newString length] >124);

 if ([text isEqualToString:@"\n"])
 {
  [textView resignFirstResponder];
  return FALSE;
 }

 return TRUE;
}
Reactions: