How to restrict user for only IntegerValue in UITextfield for iPad/iPhone?


Hi,

There are some situations where we required that User should input only numeric value like 0to9.In that kind of situation we have to use this tricks with UITextField’s delegate method.

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSNumber* candidateNumber;

NSString* candidateString = [textField.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;

}

}

Day-14 for iOS development


Click here for Lecture 14 which includes

1.UIImagePickerController( Getting still photos or video from the user either from camera or photo library)

2. MPMovie[View]PlayerController   ( How to play videos )

3. ALAssetsLibrary   (How to store images or videos in the user’s photo album)

4. Sounds  ( How to play simple sounds and record simple audio snippets)

Day-10 for iOS Development


click here for Lecture 10 which includes UITableView,UITableViewCell and UITableViewController.