In the iOS Apprentice Series eBooks, you are supposed to build a Checklist app. The tutorials have you go through and make a Done button on a button bar that is disabled and enabled based on the text inside of a UITextField object.
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
Now, the keyboard is set to disable the Done button (on the keyboard) if the text field is empty. Is there a way to observe the state of the keyboard done button and have the done button bar button reflect it accordingly?
IE, when the keyboard done button is enabled, the button bar done button is enabled.
UITextField supports what you want through the enablesReturnKeyAutomatically property. This property is from the UITextInputTraits protocol.
When you create your UITextField, set this property.
self.textField.enablesReturnKeyAutomatically = YES;
This means the Return key (whatever it is labeled) will automatically becomes disabled if the text field is empty. And it automatically becomes enabled when text is entered.
There is no way to observe the state of this so you must implement the code you already have for textField:shouldChangeCharactersInRange:replacementString: to update your other Done button.
You may add a target on your text field with the following events.
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllEditingEvents
Example:
In viewDidLoad
[_textField addTarget:self action:#selector(textFieldEditing:) forControlEvents:UIControlEventAllEditingEvents];
Action Method:
- (void)textFieldEditing:(id)sender
{
_doneButton.enable = ([[sender text] length]>0);
}
Related
I currently have my keyboard set so the type is Numbers and Punctuation [so users can type in a hypen symbol]. Does anyone know how to detect if the user presses the space bar so I can automatically set the style to a different keyboard type [in my case letters] so I don't have to build a custom keyboard?
Try this code - I am sure that it will detect if a space character is coded in - I have not really tested how well the keyboard switch works. You should also try it out without the resignfirstresponder becomefirstresponder code once the code below is working. Good Luck! (PS your viewCOntroller needs to be declared as a UITextview or textfield Delegate)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text_message {
if ([text_message isEqual:#" "]) {
// Put your code to change the keyboard then refresh the screen.
[textView resignFirstResponder]; //close textview or textfield
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder]; // opens keyboard for textfield or textview
[self.view setNeedsRefresh]; // refresh view
// return NO;
}
return YES; //go back to editing
}
Quick Question, I hope,
I have a UITextFeild that I want to type in an number and have that number populate into 6 other UITextfields on the same VC.
The first textfiled is called percentage goal while the others are named endmonth1year1percentage, endmonth2year1percentage, endmonth3year1percentage, etc.
I am currently using iOS6 with storey board.
Any help is appreciated. Thanks.
Detect the change in the first text field: UITextField text change event
And then update the text property of other text fields you want to be populated.
try like this,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//replace your textfield names here
textField1.text=textField.text;
textField2.text=textField.text;
textField3.text=textField.text;
textField4.text=textField.text;
textField5.text=textField.text;
return YES;
}
So for the first UITextField you must set the delegate the class that is responsible with the display of the text fields (a view controller or a custom view). For the other UITextFileds you should set a tag like 1,2,3..for each UITextFiled (because you say that will be lots of UITextFields)
After you set the delegate for the first UITextField and setup the tags, you can implement two different delegate methods (depending on what you want).
The method that Sunny provided, this is for instant changes:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
return YES;
}
Second, you can use the following if you want to update the textfields only after the user finished typing and the keyboard is hidden:
-(void)textFieldDidEndEditing:(UITextField *)textField {
for(int i = firstTextViewTag; i<=lastTextViewTag; i++) {
UITextView *newTextView = (UITextView *)[self.view viewWithTag:i];
//or [yourCustomView viewWithTag:i]
newTextView.text = textField.text;
}
}
EDIT
So first of all delegate is a pattern heavy used by iOS here is a tutorial that will explain the basic concept of delegate.
Second, some of the UI controls that iOS provide have a delegate instance (after you read the above tutorial you will understand why and how it's working). A class can be a delegate of a custom UI control only if the class implements the required methods that the delegate provides (NOTE: there are optional methods also in the delegate), if the class doesn't implement the required methods a build warning will be shown at the line where the delegate is set.
Third, the method used in this answer are delegate methods of the UITextFiled (check apple docs)
I lost count, [tag][3] is a property available for all UIViews of subclasses of UIView that can be used to identify an object, but be careful that this property by default is 0 so make sure when you set the tag property you will use a value > 0.
For more details please use google and Apple Docs
There is a textField and a button in my view, and if the
textfield is empty I do not want to user can click the button.
When user text something in the textfield, the button will bu
clickable.
How can I do this? Thank you.
for your UITextField, you can set:
[textField addTarget:self action:#selector(editing:) forControlEvents:UIControlEventAllEditingEvents];
Then in your editing: have:
-(void)editing:(UITextField *)sender {
myButton.enabled = ![sender.text isEqualToString:#""];
}
Listen for changes to the text field. As the text changes, update the button's enabled property based on whether there is text or not. Of course you also need to set the button's state at the beginning as well.
// Setup the text field change listener (this can be done in IB if appropriate)
// Put this in viewDidLoad if not using IB.
UITextField *myTextField = ... // a reference to the text field
[myTextField addTarget:self action:#selector(textFieldChangedAction:) forControlEvents:UIControlEventEditingChanged];
// Initialize the button's state (put this in viewDidLoad)
myButton.enabled = myTextField.text.length > 0;
// The method called as the text changes in the text field
- (void)textFieldChangedAction:(UITextField *)sender {
myButton.enabled = sender.text.length > 0;
}
If you want, you can "hide" the button by setting the alpha value of the button to 0 and when the textfield is at least one character long, then set the button's alpha value to 1 to "show" the button. I think this conceptually is easy to do and very presentable to the user.
Set the textfield.delegate to self
Then in delegate that is textfielddidbeginediting
(
Check is textfield is #""
Then disable the button
Else
Enable the button
)
And there is one more. Delegate u can use
Textdidchangecharacters
And in viewdidload first state of the button should be disabled
i have app i want that when user enters any data and text field has by default 10 so when user click on textfield then it clear the textfield to enter data.It mean when user enters data it doest not delete one bye one like clear 1 and o with back space but by default when user click textfield it should get clear.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textfield settext:#""];
}
if you focus on textfield then this method is called and textfield is cleared.
Apple Developers already did this for you, set the 10 into UITextField placeholder property see doc, when you tap on the textfield, it will be remove automatically!
However, if you want to clear it in case you've added 10 as text of textfield then,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = #"";
}
It will call each time when you focus to textfield.
I have an UITextView and I don't want check editable option, how can call keyboard via a button?
This code doesn't work for me!
-(IBAction) yourButtonClick
{
[myTextView becomeFirstResponder];
[self.view addSubview:myTextView];
}
From the iPhone Application Programming Guide
However, you can programmatically
display the keyboard for an editable
text view by calling that view’s
becomeFirstResponder method. Calling
this method makes the target view the
first responder and begins the editing
process just as if the user had tapped
on the view.
So to show the keyboard programmatically,
[textView becomeFirstResponder];
However, the keyboard will never show if the textView is not editable.
The purpose of showing the keyboard is to allow editing. I assume you just don't want the keyboard to appear when the user taps the text view. In this case, you can enable editable programmatically when the button is tapped.
-(IBAction) yourButtonClick
{
myText.editable = YES;
[myText becomeFirstResponder];
}
Then in the UITextViewDelegate, disable editable when the user finishes editing.
- (void)textViewDidEndEditing:(UITextView *)textView {
textView.editable = NO;
}