IPhone Objective-C only enable a button if NSTextfield is not blank - iphone

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;
}

Related

How get value from one UITextField to another UITextField ?

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.

UITextField Should accept numbers only in iphone

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.

return textfield keyboard on editing

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;
}
}

How to set limit in text field?

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;
}

UITextfield chacter limit in iphone

In My app i need to post the value of uitextfield as soon as user enter the fourth character.
how can get the value of uitextfield when the user enter the fourth character.
Can any one tell me how can i do it?
Set the UITextField's delegate to the view controller/file's owner and use the textViewDidChange delegate method.
- (void)textViewDidChange:(UITextView *)textView {
if ([textView.text length] == 4) {
// Your post action goes here, with the value being textView.text
}
}
u can use UITextField delegate method -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
it calls whenever user enter char ..