Grab Text From Dynamically Created UITextField - iphone

I am trying to grab the text from a dynamically created text field. I use this to make the text field become and resign first responder:
[(UITextField *)[self.view viewWithTag:0] becomeFirstResponder];
That works fine, but when I try to get the text, the app crashes.
[(UITextField *)[self.view viewWithTag:0] text];
What am I doing wrong?

Do not use the tag 0. viewWithTag: searches the view hierarchy starting from itself and since all views start with tag 0, it will identify itself as the view to be returned.
I suggest that you use a different tag on the text field.

Since the default tag for every UIView is 0, I'm going to guess that there are multiple UIViews with the same tag (tags aren't guaranteed to be unique). Instead, choose an arbitrary high value like 1000, then increment that with each view added.
Also, it would help if you included your UITextField creation code.

Related

Access UITextField with tag on a static Table

So I have a static TableView setup in the interface builder with multiple sections, labels, and text fields.
I'd like to be able to something along the lines of
[self.view viewWithTag:kNameTextfield]
and get the UITextField with that tag that I've set.
For some reason I cannot access the UITextField and it comes up Nil. I imagine because it's somewhat deeply nested? I thought about creating an Outlet collection of all the text fields and other than iterating through them every single time I need to change something seems like a waste?
What I'm trying to do is on initial load, it populates the values of the textfield by values in a dictionary. So it's useful to be able to target a specific textfield.
Try iterating through the subviews:
for (UIView* viewsLevel1 in [cell.contentView subviews])
{
for (UIView* viewsLevel2 in viewsLevel1)
{
UITextField *textField = [viewsLevel2 viewWithTag:kNameTextField];
}
}
Go as far deep as you need to go

How to know which tableview cell's text field is changed its content

I have a tableview with around 100 text fields inside it. I added only one text field. It is reused and it is accessed using tag.
The problem is this: suppose the user has changed some textfield. Now, I want to access that text field changes during saving of the data.
How can I do this?
You are very near to your goal. You have already assigned tag to your UITextField. You can use and track tag in this delegate method.
-(void)textFieldDidEndEditing:(UITextField *)textField
{
}
First set tag of textfield with indexpath.row
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// replace text in your array
}

How to handle references to UITextFields in dynamic UITableView (iPhone)

I have a UITableView that is broken up into a user defined number of sections. Within each of these sections there are always 2 rows. Each of these two rows contains a UITextField which the user is able to edit.
What I need is a way of being able to access the data in these UITextFields at a later point. I was hoping it would be a simple problem however it's causing me a great deal of grief.
So far I have tried two approaches:
Attempt 1
I created two NSMutableArrays in which I added the UITextField objects to at index i (corresponding to the section it came from). I then tried to access the values by iterating through the array. This didn't work since the UITextFields kept getting wiped clean. Every-time I scroll down the table and the UITextField is out of view, when I go back it's contents have been wiped clean.
Attempt 2
I tried to get hold of the number of sections in the UITableView (this was fine). I then wanted to iterate through each section of the UITableView, recording the values in the rows of each. This is where I became unstuck since I'm not sure how to do this or if it's even possible.
I apologise if this is a naive question to ask, however I'm really struggling and would appreciate any advice.
Keep in mind that the text fields get reused as you scroll, so you don't really want to store references to them.
What you do instead, is to capture the information as it is entered. The easiest way to do this is to implement the textFieldDidEndEditing protocol method in the delegate.
The tricky part is figuring out which row the text field is in. The best way is to create a UITableViewCell subclass which has a NSIndexPath property. You can then set that when you configure the cell with tableview:willDisplayCell:forRowAtIndexPath:.
Then, in textFieldDidEndEditing, access the tableViewCell indexPath property through its superview. i.e.:
NSIndexPath indexPathOfParentCell = [(MyUITableViewCellSubclass *)self.superview indexPath];
Doing it this way allows you to know both the section and row of the cell.
Create your TextField in the cellForRow of the Table like so and give it a tag
UITextField * userField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, self.view.frame.size.width -20, 20)] autorelease];
userField.tag = 1001;
userField.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
userField.textAlignment = UITextAlignmentCenter;
userField.delegate = self;
userField.autocorrectionType = UITextAutocorrectionTypeNo;
userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
userField.clearButtonMode = UITextFieldViewModeWhileEditing;
if (indexPath.row == 0)
[cell.contentView addSubview:userField];
then access the TextField like so:
UITextField *userField = (UITextField *)[[(UITableViewCell *)[(UITableView *)tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] contentView] viewWithTag:1001];
Your "Attempt 1" should work OK if you keep the text field's text in your arrays rather than the text field itself. (Anything that tries to make a view or control into a data object has a good chance of going wrong.)
Whatever acts as a data source for your table view should be able to re-populate the scrolled cells according to section and row if the content is stored separately.

How i can fetch what is typed between 2 UITextViews on iphone?

i have two UITextView items, how can i fetch what is written using a button on iphone?
Imagine something like a translate app, the user enters a word in UITextView 1 and by pressing the button the UITextView 2 is getting filled with data.
UITextView has a property text. Simply use this.
Set up IBOutlets for textView1 and textView2. Then have the button do something along these lines:
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:textView1.text];
}
To get fancier, you can have a method -(NSString *)transformText:(NSString *)text that translates or does whatever you like. Then use
-(IBAction)moveTextOver:(id)sender {
[textView2 setText:[self transformText:textView1.text]];
}
Create an IBAction method that is linked to a button and in that method read the "text" property of the textView or textField, do your calculations on it and assign the results to the text property of thee second field.

How I update a scroll view?

I, sorry for my english I'm not very good, I code in objective-c, I try to set text in a uiscrollview and update the content of the scrollview right after. I have a loop and I set the text with the method [uiscrollview setText:] in that loop, but the text is only displayed in the scrollview at the end of the loop...
thanks
Alex
Since when the UIScrollView has a text property?.. Do you mean UITextView?
If the loop is in main thread then the UI will probably be updated only after completing the method (with the loop inside) - just like you write. Possible solution might be to execute the method with the loop in the background thread (e.g. [self performSelectorInBackground:#selector(updateScrollViewInLoop) withObject:nil];) and inside the loop update the scroll view in the main thread ([uiscrollview performSelectorOnMainThread:#selector(setText:) withObject:text waitUntilDone:YES];).
You might also add [uiscrollview setNeedsDisplay]; line right after updating the text. I'm not sure it will help because of the second point above and, in addition, I think that this line is called in the background anyway once you change the content of the scroll view...
You first need to build your string, then call set text. Set text first clears the existing text. Also, I am assuming you mean UITextView (a subclass of UIScrollView) as it has a 'text' property. Try:
NSString *text = [NSString string];
for (NSString *line in lines)
{
text = [text stringByAppendingString:line];
}
[self.textScrollView setText:text];