How to find out programmatically added textfields in a scrollview - iphone

Inside my scrollView i am adding uitextfields programmatically by clicking an "ADD" Button. the number of textfields depending on how much i press in add button. it can be 'N' number of textfields.
After this fields editing how can i get values inside the all textfields into an array.
if once i edited textfield there is a possibility to delete entry inside that. So what is the way to save values in the all text fields while clicking in Save button.
i am attaching my screenshot with this.
Thanks in Advance

use fast enumeration.
NSMutableArray *arr = [NSMutablearray....];
for (UIView *subV in self.view.subviews){
if([subV isKindOfClass:[UITextField class]]){
//store it in a NSDictionary, so later can still know which
//textField your text belongs,
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectAndKey:subV.txt
,subV.tag,/*or subVw.placeholder*/,nil];
[arr addObject:tempDic];
}
}

You can get all the textfields by iterating through the subview array of the scrollview
for (UIView *subView in scrollview) {
//check if the subView is a textfield by for example matching a certain tag
//save text of textfield
}

When you are creating textField then assign tagValue to every textField and then access textField using tag value....
UITextField *txtField = (UITextField *) [scrollView viewWithTag:tag];

Related

retrieve text from textfield

i have
if(indexPath.row==0)
{
cell.lbl.text=#"abc";
cell.txtField.placeholder=#"abc"
cell.txtField.tag=104;
delegate.copyNameString = [NSString stringWithFormat:#"%#", [cell.txtField text]];
} else if(indexPath.row==1)
{
cell.lbl.text=#"home";
cell.txtField.placeholder=#"xyz";
cell.txtField.tag=105;
}
I am trying to retrieve the text which is in textfield, at row 0 and row 1.
i am trying to retrieve text on the textfield on the basis of tag.
How can i retrieve text?
Thanks
You can retrive your textfield like this and when you got your required textfield ,then get text.
UITableViewCell *myCell = (UITableViewCell *)[*yourTableViewName* cellForRowAtIndexPath:*passindexPath*]];
UITextField *mytextFiled = (UITextField *)[myCell viewWithTag:104];
the direct answer to your question is to use viewWithTag. In your example, [cell viewWithTag:105] will give you the text field. That said, your example has other issues.
First, if you can access the cell and the cell has a txtField property than you don't need the tag. Just use cell.txtField when you have the cell. Tags are typically used when you don't have a property holding a reference to the view you need. If you can't access the cell, the tag won't help you.
Second, you shouldn't add subviews directly to the cell. Instead you should add them to the cells contentView. The docs explain how to create a custom UITableViewCell fairly well.
NSString *textValue=((UITextField *)[self.view viewWithTag:104]).text;
You do like this,
UITextField *urtextFiled = (UITextField *)[self.view viewWithTag:104];
NSString *textValue=[urtextFiled text];
In which method you are trying to retrieve the text? I think you have to set delegate for your textfield and retrieve the text from this delegate method -(BOOL) textFieldShouldReturn:(UITextField *)textField

objective-c: How to access textfield in first row of second section of Tableview?

I have a Tableview I want to change the value of textField on the first row of second section?
I assign it tag value which lets say is 2.
Now how to access that TextField with tag.
Give a tag to that text field and by using that tag you can change the value.
UITextField *tempField = (UITextField *) [self.view viewWithTag: tag];
tempField.text = #"Your data";
If you wnat to access your textField inside some table view methods so you can do this:-
UITextField *tView=(UITextField *)[cell.contentView viewWithTag:2];
If you have to access your textField outside some tableVIew method than you have to do :-
lastSelectedPath=indexPath;do where you have added your text field.You will need its indexPath.Make it a global variable.
UITableViewCell *prevoiusCell=(UITableViewCell *)[*yourtabelviewname* cellForRowAtIndexPath:lastSelectedPath];
UITextField *tView=(UITextField *)[prevoiusCell.contentView viewWithTag:2];

Retrieve UITextField Text for a specific UITextField Tag

Hey
Just wondering how do you..
Iv created some code that automatically creates a certain amount of UITextField input by the user.
Each UITextfield has a set tag automatically created.
The user then inputs his value into each of the UITextFields.
I want to retrieve the input text from the UITextFields which correspond to the tag.
I thought I nearly had it with:
NSString * tmpV = (NSString*)[choiceTextField.text viewWithTag:result];
where result is a increment, choiceTextField is the UITextField. With this I get a problem not defining instance, which I can't do as the UITextFields are generated in code not on the xib.
So to sum up, basically want the retrieve the text from a UITextField with specific tag.
Thanks
Dan
UITextField *yourTextField = (UITextField *)[self.view viewWithTag:result];
NSString *getText = myTextField.text;
Hope this helps
Your textfields must be a subview of a parent view. You need to write the following in your view controller of the parent view:
NSString *text = ((UITextField*)[self.view viewWithTag:result]).text;
Just go with Hetal Vora's solution: looks much cleaner!
-viewWithTag can be called on any view and searches all its subviews for one that matches the tag. Thus, you will find your UITextField by calling it on any view that is above it in the hierarchy. For example, you could call it on the view controller's view that contains your text field, as follows:
NSString *tmpV = [[myViewController.view viewWithTag:result] text];
If you are already in the view controller's class you could use:
NSString *tmpV = [[self.view viewWithTag:result] text];
On second thought, the following may be more correct & will avoid any compiler errors:
UITextField *myTextField = (UITextField *)[self.view viewWithTag:result];
NSString *tmpV = myTextField.text;

How to remove everything fron the superView and not just the last item?

In my app i had to draw certain checkboxes at a same time and i used a single function to add all of them. Now when a user clicks one of them all of those checkboxes should get removed from the superview and currently its just removing the last one. Also i have issue to recognize those checkboxes like which one is clicked. i know it should be done through Tag property but don't know how exactly it should be implemented.
Any suggestions.
Removing all subviews
int numberOfSubviews = [[yourView subviews] count];
for(int i=0;i<numberOfSubviews-1;i++
{
[[youView subviews]objectAtIndex:i]removeFromSuperView];
}
//this will leave check box that you added at last.... for first one to remain loop from 1 to numberOfSubviews....
Using tag property...
when you are creating checkbox objects use
checkBoxObject.tag = i;
//I am considering i as looop count which you are using in a loop
to add checkboxes.
then whenever you need a object of checkbox
[yourViewonwhichYouAddedCheckBox viewWithTag:<your tag >];
Thanks
For identifying a "checkbox" or better said any view within an action-method:
- (void)someActionHandler:(id)sender
{
UIView *actionOriginView = (UIView *)sender;
NSLog(#"this action came from view:%d", actionOriginView.tag);
}
For assigning the tag, you may use the IB or within your code, while instantiating;
UIView *myFunkyView = [[UIView alloc] initWithFrame:CGRectZero];
myFunkyView.tag = 1337;
For removing a bunch of views from your superview - lets assume their tag is set to 10 - 15;
for (int i=10;i <= 15;i++)
{
UIView *childView = [superview viewWithTag:i];
[childView removeFromSuperview];
}

How to make UITextView scroll up when entering it?

I have a UITextView included in a UITableViewCell. The layout is correct when the views are initially displayed, but once I click in the UITextView it DOES NOT automatically scrolls up and the whole UITextView becomes invisible.
This image is when the UITextView is not active:
http://img13.imageshack.us/img13/3894/unloaded.png
And this one is when I clicked in the UITextView to make it active:
img337.imageshack.us/img337/2583/loaded.png (Put "http://" before the link, I can't post more than one Hyperlink)
I want the cell with the UITextView to scroll up. How can I achieve this?
Any suggestions are appreciated.
Julio Cezar
The simplest way would be to make an IBOutlet for the items you want to move up, and then in the code do something like this when your text field is selected:
myTextView.center.y = 200 or whatever y value you see fit.
I've already done it. My solution is not the best but it works very well: everything happens on tableView:cellForRowAtIndexPath:
1) For every cell that has a UITextView I have a NSDictionary: I save all my UITextView's on the cells on NSDictionary with a particular key.
2) And I also have another NSDictionary with the indexPath objects that are created in tableView:cellForRowAtIndexPath. If a cell A has UITextView A, they will have the same key in the NSDictionaries.
3) I implement the UITextViewDelegate and call textViewDidBeginEditing: here is my code:
(void)textFieldDidBeginEditing: (UITextField *)textField {
NSArray *keys = [self.answersDic allKeysForObject:textField];
id *key = (id *)[keys objectAtIndex:0];
NSIndexPath *tempIndex=[self.indexPathForViews objectForKey:key];
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
OBSERVATIONS:
a) answersDic is the NSDictionary where my UITextViews are saved.
b) indexPathForViews is the NSDictionary where my IndexPaths are saved.
c) the most important part of this code: the method, that brings the view to scroll up:
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
So, thats it. Thanks for your help!