UITextField have to accept the numbers only by using UITableView.
You have to perform two steps:
Provide keyboard type as UIKeyboardTypeNumberPad, as mentioned by others.
In
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
method check that entered string in numeric or not.You can use following method to check numeric value:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([self isNumeric:string])
return TRUE;
else
return FALSE;
}
-(BOOL)isNumeric:(NSString*)inputString
{
NSCharacterSet *cs=[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet];
NSString *filtered;
filtered = [[inputString componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:#""];
return [inputString isEqualToString:filtered];
}
In your xib file select the textFiled and in the property list there is a field Keyboard select Number Pad
If you have textFiled available while coding, set keyboardType property of UITextField.
textField.keyboardType = UIKeyboardTypeNumberPad;
Or you can set the same with in xib file.
Related
I have 2 UITextFields. one is for $ and another is for Riel. when I type in textfield$, I want to see value in textfieldRiel automatically. How to do that ?
Implement uitextfielddelegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textFieldRiel.text = textField.text;
return YES;
}
Take a look at the UITextFieldDelegate protocol, conform to it and make your controller class the delegate of the first ($) UITextField. Then for example in the textField:shouldChangeCharactersInRange:replacementString: method, which gets called whenever the user types a new character, populate the other UITextField according to your requirements!
First Declare the in your interface file.
Then implement uitextfielddelegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textfieldRiel.text=string;
return TRUE;
}
just a simple thinks you need to implement the delegates methods of the UiTextfields.
below is the code for the when type into one textfield that text also write automatically into the Riel textfield.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
self.txtRiel.text=textField.text
return YES;
}
Also Refer this method in Apple Documentation
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
Also refer the Apple Documentation here.
New to iOS development. Need help regarding UITextField delegate methods.
my problem is I have UITextField
if i enter some "XYZ" text in textfield it should display some other text like which i want while i'm editing only it should done.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.requestTextFiled setReturnKeyType:UIReturnKeyDone];
[NSTimer scheduledTimerWithTimeInterval:.1 target:nil selector:#selector(checkingTheQuestion) userInfo:nil repeats:YES];
}
-(void)checkingTheQuestion
{
}
I tried if by call checkingTheQuestion but I'm not able to do.
Please Help me.
Thanks in advance.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
is the method for you. You can use it as :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Your code
if([textField.text isEqualToString:#"xyz"])
{
textField.text = #"Text you want to set";
}
}
This method is called when you type any character on keypad.
I am working on iphone application in which i have to return the keyboard when i edit my textfield or delete the text in textfield also my textfield should allow only one character in textfield.
Thanks
Implement UITextFieldDelegate protocol and implement the following method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
You can get the length of the text in the textfield. If it is greater than 1, then, resign the first responder.
You can do something like this, considering _textField as your textfield object.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if( textField == _textField )
{
if( [textField.text length] == 0 )
{
return YES;
}
else
{
[textField resignFirstResponder];
return NO;
}
}
else
{
return YES;
}
}
can any body tell me how to set the limit for words to enter in a text field in objective-c?
In my case i have a registration form and in registration form every user have to enter username, password, email and phone no. and now i have to restrict the user to enter a name of maximum 15 characters.
can any one tell me how i can do this?
You are able to restrict number of inputs in UITextField both following ways.
First of all, set relevant UIViewController as a delegate object to UITextField
yourTextField.delegate = self;
Then add the following code snippet to your UIViewController.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.text length]<15) {
return YES;
}
return NO;
}
Or you can define UITextFieldDelegate protocol in your UIViewController interface
#interface YourViewController()<UITextFieldDelegate>
// YourViewController.m
#end
and then implement the following shouldChangeCharactersInRange task in your class.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 15) ? NO : YES;
}
Try this:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *textFieldText = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([textFieldText length] > 15)
{
return NO;
}
return YES;
}
I have a button that I only want enabled when a textfield is not blank. I have been trying to do this by implementing the following method, without any luck.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
Is there another delegate method that I could implement that gets fired after the text is changed? In this method is there some way to tell what key is being pressed, if it's a delete or something like that?
You can just check for string.length == 0 and range.length > 0;. This denotes a deletion. However, what you really want is to just do the pending modification and then check that you still have a non-empty string.
Something like this (typed in, not compiled):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *testString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if( testString.length )
myButton.enabled = YES;
else
myButton.enabled = NO;
return YES;
}