I am creating an iphone app, and I am using a UITextField. In my textfield, if the user fills the textfield, I would still like the user to see what they are text. I am not using the keyboard to fill the textfield - the values are getting filled in from buttons on a calculator. It there a way to make the textfield show the most recently entered values as it normally would with the keyboard?
Thanks.
Here is the code that populates the textfield:
- (IBAction)number:(id)sender {
NSString *entry = [sender currentTitle];
NSLog(#"%#", entry);
if(justOpenedCalculator){
total.text = #"";
total.text = [total.text stringByAppendingString:entry];
addFirstValueToDiscount = YES;
}else{
total.text = [total.text stringByAppendingString:entry];
addFirstValueToDiscount = YES;
}
justOpenedCalculator = NO;
}
I don't see why you could not do it with the textField. Just go over the UITextFieldDelegate methods in the Apple Documentation, and implement it. Here is the LINK !. You need to implement that and instead of doing a justOpenedCalculator you could utilize the textFieldDidBeginEditing method, and other delegate methods for your functions.
Make sure that you do a yourTextField.inputView = yourCustomKeyboard.
Make a
#property (nonatomic, retain) IBOutlet UITextField ...
And just assign the value to your textfield.
Related
I have two views, and i'm trying to show text that i getting from first view in UITextField of another . Second view shown by - (source) so methods ViewWillAppear and ViewDidLoad won't work. And viewDidLoad method of second view is runs when app is started.
I'm tried to make method of second class
secondClass.h:
#property (strong, nonatomic) IBOutlet UITextField *itemName;//all hooked up in storyboard
-(void)SetName:(NSString *)name;
secondClass.m:
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = name;//itemName - textField
}
and use it in first one:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
[secondView SetName:#"Bill"];
NSlog shows "Bill" but textField.text won't change anything.
My guess that app shows UITextField without changes because it shows second view that it gets from viewDidLoad method and i need to update it somehow
My question: What is the best approach to change attributes of UI elements from different classes?
Easiest way:
secondViewConroller.h :
#property NSString * conversationName;
secondViewConroller.m :
#synthesize conversationName;
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = conversationName
}
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
conversationName = #"Set this text";
[secondView SetName:#"Bill"];
I would suggest you to read about Protocols after that.
Easiest way:
in
secondViewConroller.h :
#property (nonatomic, retain) NSString *stringName;
secondViewConroller.m :
#synthesize stringName;
and in viewDidLoad method you write this line
itemName.text = stringName
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
secondView.stringName = #"Set this text";
guess there is something wrong with your itemName variable if it appears to get to the nslog.
did you create a referencing outlet in the interface builder for the textfield?
otherwise you can get the right textfield by tag, in IB put for instance tag 1 on the textfield and do in code:
UITextField *tf=(UITextField*)[self.view viewWithTag:1];
tf.text=name;
(replace self.view for the view holding the textfield, if not directly in the main view)
So i found a solution: There's was something wrong with calling method SetName: with parameters that i getting from first UIViewController.
Basically the solution is : create NSObject and put in there value from first UIViewConroller and then use it in second.
This TUTORIAL helped me to resolve the problem.
I am using the approach described in this stackoverflow posting to retrieve values from a textfield. My problem is that the tableview is presented modally and I have a save button that validates the input and stores it.
The problem is that the textFieldDidEndEditing method is not called when the user clicks an UIBarButtonItem (= the save button, which closes the modal view).
In this event (when the user wants to save the input) I would like to validate it. But the values are stored in properties in the textFieldDidEndEditing. Due to the fact that this method is not called, I cannot validate the input values correctly.
Does anyone have a hint or solution on this?
Thanks in advance!
You should assign unique tag numbers to your text fields, then keep track on which is currently active (i.e. use a int iVar to store the active text fields tag value) in the textFieldDidBeginEditing delegate and when the user clicks the save, you should get the last textfield by it's tag value and then it's text value so you can validate it.
Okay, here we go:
Thanks to #Lefteris and his idea with storing the current index. Due to the fact that I cannot store the index into the tag attribute I decided to store the active indexPath and additionally the active textField. (I know, a reference to the UITextField would have been enough but I needed it for other stuff)
First I have added these two properties:
#property (nonatomic, strong) NSIndexPath *activeIndexPath;
#property (nonatomic, strong) UITextField *activeTextField;
Then I implemented textFieldDidBeginEditing: and textFieldDidEndEditing: of UITextFieldDelegate.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSIndexPath *indexPath = (NSIndexPath*)[self.tableView indexPathForCell:(UITableViewCell*)[[textField superview] superview]];
self.activeTextField = textField;
self.activeIndexPath = indexPath;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *input = textField.text;
//assuming values from input textfield into corresponding properties
[self assumeInput:input withIndexPath:self.activeIndexPath];
self.activeTextField = nil;
self.activeTextField = nil;
}
In textFieldDidEndEditing: I am storing the values into my properties (such as self.firstName, self.lastName, and so on...) by using the method [self assumeInput:input withIndexPath:self.activeIndexPath];.
In my saveAction-Method I am storing the value from the currently active TextField.
- (IBAction)saveButtonClicked:(UIBarButtonItem *)sender
{
//assuming input from active field (didEndEditing _not_ called right now!)
[self assumeInput:self.activeTextField.text withIndexPath:self.activeIndexPath];
//test output
NSLog(#"firstName: %#", self.firstName);
NSLog(#"lastName: %#", self.lastName);
NSLog(#"email: %#", self.email);
...
}
... and that's it!
Hope it helps! Thanks to #Lefteris for his input.
Best,
Chris
I am a beginner of iOS and I am writing some practice code in which I am trying to pass a string from one tab to another in a UITabViewController and using a label to display it in the next tab. Now the code where I am passing the message is:
-(IBAction)sendMessage:(id)sender
{
MessageRecepientViewController * contoller = [self.tabBarController.viewControllers objectAtIndex:1];
[contoller passString:_textField.text];
self.tabBarController.selectedIndex = 1;
}
And I am receiving the text here (in the second view controller) as:
-(void) passString:(NSString *) str
{
_string = str;
}
And in viewDidAppear, i am doing this:
- (void) viewDidAppear:(BOOL)animated
{
self.textLabel.text = [NSString stringWithString:_string];
NSLog(#"did appear called, str = %# and label text = %#", _string, self.textLabel.text);
}
and the log is showing that the value of the string is the passed text as it should be, but the value of textLabel.text is always null.
I have tried everything I could think of from checking that the UILabel is attached to the textLabel outlet to writing the code in viewWillAppear and viewDidLoad but nothing have worked so far. The string is showing the correct value but the label is not updating. What is wrong here?
Ok found the error:
It was a minor typo: I had declared IBOutlet as:
IBOutlet UILabel * textlabel;
and property as:
#property (nonatomic, strong) UILabel * textLabel;
and had attached the outlet to textlabel and had been updating textLabel. Fixed this and things are fine!
I need a way of determining the UITextField that is currently selected in a view. Is this possible without passing a reference or tag?
To be more specific I need to be able to tell which UITextField is selected so that I can hide the keyboard. The reason I need this is because I want to create a UIToolbar to add to all the UITextField's as an input accessory. On this UIToolbar I will add a 'Done' button, when pressed this should hide the keyboard for the currently selected UITextField.
I assume you mean you want to know which UITextField is the first responder (which is the text field that gets input from the keyboard).
There is no public API for this (though there is a private API). You can track which text field is the first responder manually using the textFieldDidBeginEditing: method of each text field's delegate, or you can use a little trickery to find the first responder at any time.
Here's the trick. The UIApplication object knows which object is the first responder, and can send a message to it. So you write a category like this on UIResponder:
UIResponder+firstResponderHack.h
#import <UIKit/UIKit.h>
#interface UIResponder (firstResponderHack)
+ (UIResponder *)firstResponderByHack;
#end
UIResponder+firstResponderHack.m
#import "UIResponder+firstResponderHack.h"
#interface FirstResponderFinder : NSObject
#property (strong, nonatomic) UIResponder *firstResponder;
#end
#implementation FirstResponderFinder
#synthesize firstResponder = _firstResponder;
#end
#implementation UIResponder (firstResponderHack)
- (void)putFirstResponderIntoFinder:(FirstResponderFinder *)finder {
if (self.isFirstResponder)
finder.firstResponder = self;
}
+ (UIResponder *)firstResponderByHack {
FirstResponderFinder *finder = [FirstResponderFinder new];
// Sending an action to nil sends it to the first responder.
[[UIApplication sharedApplication] sendAction:#selector(putFirstResponderIntoFinder:) to:nil from:finder forEvent:nil];
return finder.firstResponder;
}
#end
Then you can find the first responder, and check whether it's a UITextField, like this:
UIResponder *firstResponder = [UIResponder firstResponderByHack];
if (firstResponder && [firstResponder isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)firstResponder;
// do something with textField
}
There is an easy way to dismiss the keyboard without having to track the currently active control, or iterating through all the available controls, or using a UITextFieldDelegate.
[self.view endEditing:YES]
From the docs:
endEditing:
Causes the view (or one of its embedded text fields) to
resign the first responder status.
- (BOOL)endEditing:(BOOL)force
Parameters
force
Specify YES to force the first responder to resign, regardless of whether it wants to do
so.
Return Value
YES if the view resigned the first responder status or NO if it did not.
Discussion
This method looks at the current view and its subview
hierarchy for the text field that is currently the first responder. If
it finds one, it asks that text field to resign as first responder. If
the force parameter is set to YES, the text field is never even asked;
it is forced to resign.
There is a delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
Apple Docs:
This method notifies the delegate that the specified text field just
became the first responder. You can use this method to update your
delegate’s state information. For example, you might use this method
to show overlay views that should be visible while editing.
There is also a property:
#property(nonatomic, readonly, getter=isEditing) BOOL editing
Apple Docs:
A Boolean value indicating whether the text field is currently in edit
mode. (read-only)
Just make an ivar for the UITextView in your header file:
UITextField *editingField;
#property (nonatomic, copy) UITextField *editingField;
Then,
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
editingField = textField;
// Whatever else you want to do
}
I'm thinking that you need to diff the textFields without reference.
So, the recommended why is using ObjectiveC runtime.
It's pretty straight forward.
Firstly:
#import <objc/runtime.h>
Then, define a char for its address:
static char UITextFieldViewIdentifier;
Then set the identifier with something like this:
objc_setValue(textField, &UITextFieldViewIdentifier, #"Identifier") //typing on a phone, not so sure about the expression
In the delegate method:
NSString *identifier = objc_getObject(textField, &UITextFieldViewIdentifier)
Just call this line where you want to dismiss the keyboard:
[self.view endEditing:YES];
I use the "Next" value for the "Return Key" to get the Next button in place of the Done button, but (obviously) pressing it doesn't automatically move to the next UITextField in my view.
What's the right way to do this? On a larger topic, what are some tips for properly building forms in the iPhone SDK?
Make some object the first text field's delegate, and implement the - (BOOL)textFieldShouldReturn:(UITextField *)textField method; in that, call the second text field's -becomeFirstResponder. Returning YES from that will make the text field perform its default behavior for the return button – I think that's generally sending its action message. If you don't have anything added as a target of that action, it doesn't really matter what you return.
To build on Noah's answer, if you have a lot of textfields and don't feel like having a bunch of if's, you could do it this way:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//[[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
UIView *view = [self.view viewWithTag:textField.tag + 1];
if (!view)
[textField resignFirstResponder];
else
[view becomeFirstResponder];
return YES;
}
Once you tag every textfield starting at any number, as long as they're tagged sequentially, in storyboard or in code, it should work.
For Swift:
func textFieldShouldReturn(textField: UITextField) -> Bool {
//your collection of textfields
guard let i = textFields.indexOf(textField) else { return false }
if i + 1 < textFields.count {
textFields[i + 1].becomeFirstResponder()
return true
}
textField.resignFirstResponder()
return true
}
This seems to work quite well and doesn't require the tag system many are suggesting. There are 2 things to note with this solution though:
All the UITextFields must be in the same UIView (have the same superview).
The UITextFields need to be in the right order in the interface builder.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
/*
* 1. Loop through the textfield's superview
* 2. Get the next textfield in the superview
* 3. Focus that textfield
*/
UIView *superView = [textField superview];
BOOL foundCurrent = false;
for (UITextField *tf in superView.subviews) {
// Set focus on the next textfield
if (foundCurrent) {
[tf becomeFirstResponder];
return NO;
}
//Find current textfield
if ([tf isEqual:textField]) {
foundCurrent = true;
}
}
return YES;
}
I don't like to deal with tag so here is my solution. Create an IBOutletCollection of all your textFields in your ViewController, drag to connect your textFields in order from top to bottom.
#interface ViewController () <UITextFieldDelegate>
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *allTextFields;
#end
In viewDidLoad set your textFields delegate. (Or set it in storyboard).
for (VVTextField *tf in self.allTextFields) {
tf.delegate = self;
}
Then implement UITextField Delegate
#pragma mark - UITextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSUInteger currentIndex = [self.allTextFields indexOfObject:textField];
NSUInteger nextIndex = currentIndex+1;
if (nextIndex < self.allTextFields.count) {
[[self.allTextFields objectAtIndex:nextIndex] becomeFirstResponder];
} else {
[[self.allTextFields objectAtIndex:currentIndex] resignFirstResponder];
}
return YES;
}
I've been struggling with this issue too...and as a result I've created small library for handling multiple textfields. You can find it on github GNKeyboardAwareScrollView#GNTextFieldsManager.
You can initialise it with array of textfields:
NSArray *myTextFields = #[...]; // the order of array matters!
GNTextFieldsManager *manager = [[GNTextFieldsManager alloc] initWithTextFields:myTextFields];
Or by specifying parent view (and setting tags for all views):
GNTextFieldsManager *manager = [[GNTextFieldsManager alloc] initWithView:self.view];
Hope i'll be useful for somebody :)