Pregunta

I am working in a project where I have draw a form multiple times. I mean, its an app where I have to take same details from user for different person involved in it. Suppose at first user input that there are 4 person, then I need to draw a form 4 times in scroll view so that user can enter details of all 4 people. To achieve this, I have create a view of form (which includes 3 UITextFields) in XIB and repeating that view dynamically depending upon number of people. This is working fine as per the requirement and I am able to take show the form to the user. But I have a little confusion of getting the value from all UITextfields and then saving all values at backend so that I can use it. The formview is added to UIScrollView and UIScrollView is added to self.view. Every form view contains a tag value which I added while the adding the view to self.view.

But how can I get the values from UITextFields which are added to formview. Please suggest.

¿Fue útil?

Solución

If you need to access values of textfields from scrollview forms, you can use this code for each scrollview object while retrieving and saving all the data:

//to get scrollview object from self.view
UIScrollView *scrollView1 = (UIScrollView*)[self.view viewWithTag:SCROLL_VIEW_TAG_1];

//to get formview from scrollview object
UIView *formView1 = (UIView*)[scrollView1 viewWithTag:FORM_VIEW_TAG_1];

//to get textfields from formview
UITextField *textField1 = (UITextField*)[formView1 viewWithTag:TEXT_FIELD_TAG_1];
NSString *value1 = textField1.text;
UITextField *textField2 = (UITextField*)[formView1 viewWithTag:TEXT_FIELD_TAG_2];
NSString *value2 = textField2.text;

//same for other textfields and scrollviews

I hope it works for you. The main thing you need is to properly assign tag values to different fields and scrollviews.

Otros consejos

If your structure is like following then it will not an issue...

Scrollview --> form1(tag == 0) --> All text fields (tag starts from 1 to 4) | | --> form 2(tag ==1) --> All textfield (tag starts from 1 to 4)

So ideally create separate view for each person and add that in scroll view. And it will be more helpful if you create subclass of UIView for form so you have diff. form objects for each person and can access textfields of particular form to get values from it.

Update

if you have a diff. form object then get textfield value like

UITextField * txt = (UITextField *)[form1 viewWithTag:textFiledtag];
NSString * str = txt.text;

Where form1 is object added in scrollview for person and textFiledtag is your tag assigned to textfield when you create it and add in form1.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top