Send dynamically created textfield data in array - ios

I have dynamically created UITextfield using for loop and now i need to send the text contained in the UITextfield to an array inside button click and send back to another viewcontroller. But when I pass the data in array, then textfield data is sent blank. Please help.
Thanks.

To do this you have to use delegate method.
For Ex:
Initialize the Values array before calling this method;
for(int i=0;i<5;i++)
{
UITextField *text1 =[UITextField alloc]init];
// set frame for text1
text1.tag = i;
text1.delegate=self;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[Values setObject:textField.text atIndexedSubscript:textField.tag];
}
on passing array dont forget to add
[self.view endEditing:YES];
Please let me know if you have any help

Related

To enable button if all the textfields are filled in ios

I have designed a signup form in ios with four UITextFields and a UIButton. I have set the buttons' enabled property to NO by default. Now I want to enable the button only when all the four textfields are filled. Can you please help me out with this as I a new to ios, and stuck with this issue.
A better way would be to use the didChange method like the UITextViewDelegate method, but as we know the UITextFieldDelegate does not have a didChange method. You can manually add behaviour. You can use the shouldChangeCharactersInRange: method, but personally I would advise not to override methods unless you absolutely have to.
You can add behaviour using:
[myTextField1 addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];//And so on for all your text fields
And in the target method:
- (void)textFieldDidChange:(UITextField*)textField{
if (myTextField1.text.length > 0 && myTextField2.text.length > 0 && myTextField3.text.length > 0 && myTextField4.text.length > 0){
myButton.enabled = YES;
} else {
myButton.enabled = NO;
}
}
EDIT:
Further, if you want to make sure they are enabled only if the user has entered valid text, and not empty spaces, you may use the following to get the trimmed text, check if this trimmed text has length > 0:
NSUInteger textLength = [myString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
in your .h file
#interface YourViewController : UIViewController <UITextFieldDelegate>
in your .m file
Write this in viewDidLoad
self.btnSignUp.enable=NO; //button is disable by default
self.textField1.delegate=self; // set delegate of text field
self.textField2.delegate=self;
self.textField3.delegate=self;
self.textField4.delegate=self;
Write this text field's delegate method
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField1.text length]>0 && [textField2.text length]>0 && [textField3.text length]>0 && [textField4.text length]>0) // check if all the textfields are filled
{
self.btnSignUp.enabled:YES; // enable button here
}
}
I know this is relatively old, but I wanna pitch in another idea. I've just implemented this using the UITextFieldTextDidChangeNotification.
Register for the notification:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("textFieldDidChange:"), name: "UITextFieldTextDidChangeNotification", object: nil)
Add the method for handling the notification.
Set the enabled property of the button to true only if it's true that all of your text fields are not empty.
This wouldn't be a great solution with 20 text fields, but for 2 - 5 it's fine.
func textFieldDidChange(notification: NSNotification) {
self.button.enabled = self.firstField.text != "" && self.secondField.text != ""
}
That method gets called every time a textfield's content changes, so the button responds in real time.
I would approach this in a slightly different way.
Instead of going through the muck of enabling/disabling the UIButton, I would let the UIButton be enabled all along and let it's targeted action method decide what to do.
Example:
//Sign Up button method
-(IBAction)btnSignUp:(UIButton *)sender
{
//create a local array of the monitored textFields that should NOT be empty
NSArray *arrTextFields = #[txtF1,txtF2,txtF3,txtF4];
//helps quicken the process by using fast-enumeration as so:
for (UITextField *txtFCurrent in arrTextFields) {
if ([[txtFCurrent.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:#""]) {
NSLog(#"%# found to be empty",txtFCurrent);
//help direct the user to fill it
//maybe after showing an alert but anyways...
[txtFCurrent becomeFirstResponder];
//don't proceed with the main button logic
//since a required textField is empty
return;
}
}
//else...
NSLog(#"All textfields are go... Proceed with the sign up request");
//...
}

How to get text from textfield tag in iPhone

I am creating text fields dynamically and each text field has tag.Now, I am trying to get text of textfield using that tags but, it returning UIButton and my application getting crashed.How can i get text from tags? please help me.
Here is my code to get text :
UITextField *textValue = (UITextField *)[self.view viewWithTag:textField.tag];
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
my application is crashing at this line :
NSString *chkText = textValue.text;
and error is :
-[UIButton text]: unrecognized selector sent to instance
The main problem is your view contains button as well. So when you are trying to access it. It is returning UIButton object. So it is always better that check the condition whether it is button type or textfield type object. Try below:-
id nextTextField = [self.view viewWithTag:textField.tag];
nextTextField=([nextTextField isKindOfClass:[UITextField class]]) ? nextTextField.text : nextTextField.title;
NSLog(#"%#", nextTextField);
From your code (viewWithTag:textField.tag), If you can access your textField then why are you accessing it once more? However, the below description will help you
You have assigned the same tag to one of your UIButton control in the same view. Also make sure that textField.tag is greater than Zero.
The below code will fix the crash, however, it will not resolve your issue.
UITextField *textFeild = [[self.view viewWithTag:textFieldTag] isKindOfClass:[UITextField class]]?(UITextField *)[self.view viewWithTag:textFieldTag]:nil;
if(nil != textFeild)
{
NSString *chkText = textFeild.text;
NSLog(#"entered Text %#",chkText);
}
else
{
NSLog(#"textFeild is null");
}
Set textfield tag as any unique value like "346". (Avoid using 0 as tag value). And Use this --
UITextField *textValue = nil;
id myObj = [self.view viewWithTag:yourTagValue];
if([myObj isKindOfClass:[UITextField class]])
{
textValue = (UITextField*)myObj;
NSString *chkText = textValue.text;
NSLog(#"entered Text %#",chkText);
}
When you use tag, using NS_ENUM is a good habit.
NS_ENUM(NSUInteger, TEST_TAGS)
{
TAG_TEXT_INPUT,
TAG_BTN_GO,
};
Then you can use it.
[textField setTag:TAG_TEXT_INPUT];
First check to which UIVIew you are adding this textfield as a subview.
Take that UIVIew instance and call viewWithTag method then your code works.

UITextField becomeFirstResponder

I'm trying to move my first responder on using tags in a tableview cell. I've set _txtFieldActive to pick up the active UITextFields tag. I can see this when I press the next button on the keyboard via NSLog. Now however I can't seem to figure out how to resignfirstresponder on that tag, and then move my first responder onto tag 102?? I get an error on the line of code trying to assign tag 102 to *tmp.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
switch (textField.tag)
{
case 101:
//Do Nothing do not want to close keyboard but move on to next UITextField
if (_txtFieldActive.tag == 101)
{NSLog(#"Tag = 101");
UITextField *tmp = [textField.tag == 102];
[tmp becomeFirstResponder];
}
break;
case 102:
[textField resignFirstResponder];
}
return YES;
}
Many thanks all for your help in advance for any pointers.
Jon.
To get a reference to a view in the current view's hierarchy with a given tag, we need to call viewWithTag:.
if (_txtFieldActive.tag == 101) {
NSLog(#"Tag = 101");
UITextField *tmp = [self.view viewWithTag:102];
[tmp becomeFirstResponder];
}
Try that on for size.
If this is a UITableViewController subclass rather than a UIViewController subclass, you might need [self.tableView viewWithTag:102];, but self.view should work in either case.

objective-c how to save textfield values through textFieldShouldEndEditing:

I've a UITableViewController which has Save button. At present I'm displaying row's of UITableViewCell's. UITableViewCell contains textfield . Let's say if user enters text in 1st table cell and hits "Save" then I'm losing the entered/modified text.
suppose I have a textField
((UITextField*)[cell viewWithTag:101]).placeholder = #"title";
((UITextField*)[cell viewWithTag:101]).text = arrayActivities[indexPath.row][#"title"]; //arrayActivities[indexPath.row];
((UITextField*)[cell viewWithTag:101]).keyboardType = UIKeyboardTypeDefault;
how I would save arrayActivites editable values??
Have you tried making the UITableViewController a UITextViewDelegate? So in your tableView's .h file:
UITableViewController <UITextFieldDelegate>
and in your .m file you should be able to call:
- (void)textFieldDidEndEditing:(UITextField *)textField {
[array addObject: textfield.text];
}
Keep one global dictionary dctGlobal in your class.
Set textfield.delegate=self in cellForRowAtIndexPath method.
And then get and save value for text field in following method
- (void)textFieldDidEndEditing:(UITextField *)textField
[dctGlobal setObject:textField.text forKey: textField.placeholder];
On save, Use dctGlobal for saving into database.
Thanks!

Show a single character as it is typed on a separate label?

I have a row of labels that have been programmatically instantiated, they are stored in an NSMutableArray. They don't currently contain any data. What I'm trying to do is make it so that when a user types in a character it is automatically displayed in the labels. I'm not sure how to do this. I know how to access the labels I have created [MyArray ObjectAtIndex:0] and so on, but how could I make it so that when a user types on the keyboard it formats the text (I have code for formatting) and then just appears on screen.
I need help putting each character on the screen as it is typed.
Any help would be greatly appreciated - I have a textfield (it's hidden) and the keyboard comes up by button. If that helps. :)
Thank you in advance :).
UITextField *tf;
[tf addTarget:self action:#selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
- (void)editingChanged:(UITextField *)textField {
_myHiddenLabel.text = textField.text;
}
You can add observer when Text inside UITextField changes and then access your labels and add text to it...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(changeLabelsMethod:)
name:UITextFieldTextDidChangeNotification
object:myHiddenTextField];
}
-(void)changeLabelsMethod:(UITextField*)txtField
{
Static int i=0;
if(i<[MyArray count])
{
UILabel *lbl=[MyArray ObjectAtIndex:i];
lbl.text=txtField.text;
}
else
return
i++;
}
EDIT: Refer Eugene's answer for right approach
You can get notified every time a character is typed if you set your viewController as delegate to your hidden textField and implementing UITextFieldDelegate Protocol
- (IBAction)textFieldValueChanged {
NSString *strLastChar = [txtSearch.text substringFromIndex:txtSearch.text.length-1];
UILabel *lblCurrent = [arrSearch objectAtIndex:intCurrentLblNo];
[lblCurrent setText:strLastChar];
intCurrentLblNo++;
}
take intCurrentLblNo as global variable and set intCurrentLblNo = 0; in viewdidload method
and set it...
[txtSearch addTarget:self action:#selector(textFieldValueChanged) forControlEvents:UIControlEventValueChanged];
in viewdidload method

Resources