one TextField to 6 others - ios

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

Related

how to check if focus switches from one text view to another

I've got 2 UITextFields. I can detect when the user in inside either of them with:
textFieldDidBeginEditing:(UITextField *)textField
Which works great. But i want the textfields to animate out when the user is not inside either of them. Currently I'm calling: (void)textFieldDidEndEditing:(UITextField *)textField
However, this is called even if i switch from on textfield to the other. Is there a better way to call do this?
What I would do is add a BOOL flag to detect if you should animate out your text fields or not. It would work something like this:
-(void)hideTextFields {
if (self.shouldHideTextFields) {
self.textField1.hidden = YES; // Or whatever you want to do with
self.textField2.hidden = YES; // your text fields
}
}
Declare a method that checks the BOOL flag and decides whether to hide or not the text fields
Whenever either of the textFieldDidBeginEditing:(UITextField *)textField methods are called set the BOOL flag (you can call it 'shouldHideTextFields') to NO.
Whenever either of the textFieldDidEndEditing:(UITextField *)textField methods are called set the BOOL flag to 'YES'. Also, call [self performSelector:#selector(hideTextFields) withObject:nil afterDelay:1]; to give the user a little time to select the other text field. If he/she does, the flag will be set to NO thanks to the above step.
Hope this helps!
On didend check if either of your textviews is currently the first responder with [textfield isFirstResponder] or [textfield isEditing]. There may be a slight delay when one ends and the other takes control. If that's the case then you could do this check after a slight delay using performSelector:afterDelay.
On textFieldDidEndEditing delegate method, do not just perform your disappearing animations, but do that in dispatch_async, checking if there is no textField editing right now.
In case if user just ended editing of one text field, there will be no editing textfield. But if user had switched to another textfield, it will already start editing and it can be easily checked by isFirstResponder method.

iOS 6 - Responding to Keyboard Done Button State

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

UITextview doesn't enter editing on touch

I'm having trouble getting my UITextView to become editable when it is touched.
I've included the following code per the Apple documentation.
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
NSLog(#"begin editing");
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
NSLog(#"end editing");
return NO;
}
I"ve also read through the apple documentation and have made the textview the delegate, but it still doesn't seem to be working. I'm pretty much trying to go with the notepad affect where once you are finished editing, numbers and such are hyperlinked.
In order to have that notepad effect, where numbers and links are hyperlinked, you would need to replace your UITextView with a special CoreTextView. I'd modify something like this to your needs: https://github.com/jasarien/CoreTextHyperlinkView. So in textViewShouldEndEditing: you would populate your CoreTextHyperlinkView text with textView.text, hide the textView and show your CoreTextView. And vice versa in textViewShouldBeginEditing:.
Also, make sure you reference the <UITextViewDelegate> protocol in your header.
Remember to set your textView's delegate, otherwise those methods won't get called
self.textView.delegate = self; // assuming self is the view controller with your textView
And remember <UITextViewDelegate>

I need to validate two uitextfields differently. How do I do this?

One text field only takes alphabet characters and the other one is alphanumeric. I have defined shouldChangeCharactersInRange in my ViewController for one of them. Now I don't know where I'm supposed to define the logic for other uitextfield.
Can someone help me understand how this should work?
If they both have the same delegate, then they would both be validated in the same shouldChangeCharactersInRange method. You need to put an if-else clause in that method to check which text field is the sender. You need IBOutlets for the two text fields so you can do the comparison.
You have a couple options. If you have created iVars for your text fields, checking which one is calling shouldChangeCharactersInRange is as simple as ==. Below I've also shown that another option would be to assign tags to the text fields and check the tag of the sending text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//Your first option
if (textField == myFirstTextField) {
//
}else{
//
}
//another option, if you don't want to create iVars you can assign tags to your text fields and do this
if (textField.tag == 99) {
//
}else{
//
}
}

How do you set the tab order in iOS?

Is there a way (either in IB or code) to set the tab order between text fields in a view?
Note that I'm not talking about the next form field after the return (or "Next") button is pressed -- many bluetooth keyboards have a tab key, which seems to cycle through the fields in completely different order. In my particular case, this order doesn't correspond to the fields' position in the view or even the order in which the fields were added. Modifying the xib file by hand to change the NSNextKeyView doesn't seem to make a difference either.
Does anyone know how to change this order?
#sprocket's answer was only somewhat helpful. Just because something works out of the box doesn't mean you should stop thinking about a better way -- or even the right way -- of doing something. As he noticed the behavior is undocumented but fits our needs most of the time.
This wasn't enough for me though. Think of a RTL language and tabs would still tab left-to-right, not to mention the behavior is entirely different from simulator to device (device doesn't focus the first input upon tab). Most importantly though, Apple's undocumented implementation seems to only consider views currently installed in the view hierarchy.
Think of a form in form of (no pun intended) a table view. Each cell holds a single control, hence not all form elements may be visible at the same time. Apple would just cycle back up once you reached the bottommost (on screen!) control, instead of scrolling further down. This behavior is most definitely not what we desire.
So here's what I've come up with. Your form should be managed by a view controller, and view controllers are part of the responder chain. So you're perfectly free to implement the following methods:
#pragma mark - Key Commands
- (NSArray *)keyCommands
{
static NSArray *commands;
static dispatch_once_t once;
dispatch_once(&once, ^{
UIKeyCommand *const forward = [UIKeyCommand keyCommandWithInput:#"\t" modifierFlags:0 action:#selector(tabForward:)];
UIKeyCommand *const backward = [UIKeyCommand keyCommandWithInput:#"\t" modifierFlags:UIKeyModifierShift action:#selector(tabBackward:)];
commands = #[forward, backward];
});
return commands;
}
- (void)tabForward:(UIKeyCommand *)command
{
NSArray *const controls = self.controls;
UIResponder *firstResponder = nil;
for (UIResponder *const responder in controls) {
if (firstResponder != nil && responder.canBecomeFirstResponder) {
[responder becomeFirstResponder]; return;
}
else if (responder.isFirstResponder) {
firstResponder = responder;
}
}
[controls.firstObject becomeFirstResponder];
}
- (void)tabBackward:(UIKeyCommand *)command
{
NSArray *const controls = self.controls;
UIResponder *firstResponder = nil;
for (UIResponder *const responder in controls.reverseObjectEnumerator) {
if (firstResponder != nil && responder.canBecomeFirstResponder) {
[responder becomeFirstResponder]; return;
}
else if (responder.isFirstResponder) {
firstResponder = responder;
}
}
[controls.lastObject becomeFirstResponder];
}
Additional logic for scrolling offscreen responders visible beforehand may apply.
Another advantage of this approach is that you don't need to subclass all kinds of controls you may want to display (like UITextFields) but can instead manage the logic at controller level, where, let's be honest, is the right place to do so.
I'm interested in solving the same problem, although so far the default order, which appears to be left to right, then top to bottom, is the one I want.
I tested the hypothesis that the cursor moves in depth-first order through the tree of subviews and superview, but that is not true. Changing the order of subviews without changing their location didn't change the order of fields traversed by tab presses.
One possibly useful feature is that the text field delegate's textFieldShouldBeginEditing method appears to be called for every text field in the application's window. If that returns NO, then the text field won't be chosen, so if you can define your desired order and make only the right one return YES, that might solve your problem.
This is how you set the tab order on iOS:
http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with-an-external-keyboard/
The Tab key behaviour in ios will be as follows:-
when u press tab on external keyboard- the control traverses across all the textfields in that screen by calling only shouldBeginEditing method where its return value is also determined by Apple which cant be override.
After scanning all the fields it calculates nearest x positioned Textfield relative to view offset from the current Textfield and then nearest Y Positioned Field.
Also can't be done anything until control comes to textFieldDidBeginEditing method.
Reason for apple's restriction might be to let devs to follow the guidelines of UI where next responder of field should be it's closest positioned Field rather than any other field .
Register a UIKeyCommand to detect the tab key pressed. I did this in my current view controller.
self.addKeyCommand(UIKeyCommand(input: "\t", modifierFlags: [], action: #selector(tabKeyPressed)))
Inside the key tabKeyPressed handler find your current active field then set your next responder. orderedTextFields is an array of UITextField in the tab order I want.
func tabKeyPressed(){
let activeField = getActiveField()
if(activeField == nil){
return
}
let nextResponder = getNextTextField(activeField!)
nextResponder?.becomeFirstResponder()
}
func getActiveField() -> UITextField? {
for textField in orderedTextFields {
if(textField.isFirstResponder()){
return textField
}
}
return nil
}
func getNextTextField(current: UITextField) -> UITextField? {
let index = orderedTextField.indexOf(current)
if(orderedTextField.count-1 <= index!){
return nil
}
return orderedTextField[index! + 1]
}
You can do this by setting the tag for each textfield and handling this in the textfieldShouldReturn method.
See this blogpost about it:
http://iphoneincubator.com/blog/windows-views/how-to-create-a-data-entry-screen
The only way I've found to uniquely detect a Tab keystroke from a physical keyboard, is implementing the UIKeyInput protocol's insertText: method on a custom object that canBecomeFirstResponder.
- (void)insertText:(NSString *)text {
NSLog(#"text is equal to tab character: %i", [text isEqualToString:#"\t"]);
}
I didn't get this to work while subclassing UITextField, unfortunately, as UITextField won't allow the insertText: protocol method to get called.
Might help you on the way, though..
I solved this by subclassing UITextField as NextableTextField. That subclass has a property of class UITextField with IBOutlet a hookup.
Build the interface in IB. Set the class of your text field to NextableTextField. Use the connections Inspector to drag a connection to the 'next' field you want to tab to.
In your text field delegate class, add this delegate method...
- (BOOL)textFieldShouldReturn:(UITextField *) textField
{
BOOL didResign = [textField resignFirstResponder];
if (!didResign) return NO;
if ([textField isKindOfClass:[NextableTextField class]])
dispatch_async(dispatch_get_current_queue(), ^{ [[(NextableTextField *)textField nextField] becomeFirstResponder]; });
return YES;
}
BTW - I didn't come up with this; just remember seeing someone else's idea.

Resources