Manglu
Manglu

Reputation: 11384

Creating a form dynamically in iOS

I have zero knowledge of iOS and am looking at an application that would require a form to be built dynamically based on info sent by the server side.

I am looking at it from the server side (which is Java EE based) but want to be clear on what are the constraints and practice on the iOS side which determines whether I should be heading down this path or not.

If the data sent to the iOS app looks like this:

[
    {
        "values": "Text Field",
        "required": "true",
        "hidden": "true",
        "readonly": "false",
        "prevalue": "This is text field",
        "fieldesc": "First Name"
        "mask": "false"
    },

    {
        "values": "Text Field",
        "required": "true",
        "hidden": "true",
        "readonly": "false",
        "prevalue": "01/01/1980",
        "fieldesc": "Date Of Birth"
        "mask": "false"
    },


    {
        "values": "Text Field",
        "required": "true",
        "hidden": "true",
        "readonly": "false",
        "prevalue": "888 888 888",
        "fieldesc": "Tax File Number"
        "mask": "true"
    },
]

I want the iOS app to create the appropriate fields.

This could become complex when the number of fields increase and I might have to keep in separate pages.

I wanted to hear thoughts and suggestions to see how should I be approaching this.

Upvotes: 1

Views: 1891

Answers (2)

Midhun MP
Midhun MP

Reputation: 107231

I think my solution won't be the best one, but I think you can implement it using a simple for loop.

First you need to parse the whole JSON, it'll give you an array with 3 dictionaries.

float xCord = 20;
float yCord = 40;

for (int loop = 0; loop<[parsedJsonArray count]; loop++)
{
  xCord = 20;
  //You need to create a label for displaying the description
  UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(xCord,yCord,100,20)];
  lab.text = [[parsedJsonArray objectAtIndex:0] valueForKey:@"fieldesc"];
  [self.view addSubview:lab];

  xCord = 130;
  if ([[[parsedJsonArray objectAtIndex:0] valueForKey:@"values"] isEqualToString:@"Text Field"])
 { 
   UITextField *tfd = [[UITextField alloc] initWithFrame:CGRectMake(xCord,yCord,100,20)];
   tfd.text = [[parsedJsonArray objectAtIndex:0] valueForKey:@"prevalue"];
   [self.view addSubview:tfd];
 }

 yCord += 35;
}

You need to adjust the x and y co-ordinate as you need, also do the same for the text field and other form elements.

Another point: if you need multiple page you need to handle that inside the loop. But I'll suggest it's better to put a scroll view and other element on top of that.

Upvotes: 2

melsam
melsam

Reputation: 4977

Start with a UITableViewController. Implement the UITableViewDataSource and UITableViewDelegate delegates, which will allow you to dynamically create rows based on the field metadata you're getting from the server.

You'll implement methods like cellForRowAtIndexPath, numberOfRows and numberOfSections to create UITableViewCells for each field in your form.

You'll insert UITextField controls into each cell of the tableview. Ultimately you'll end up with a scrollable, editable form full of textfields embedded inside the tableview.

Upvotes: 3

Related Questions