Rahul Umap
Rahul Umap

Reputation: 2869

Creating Forms dynamically depend upon the response from server

I'm working on one app in which the UI rendering will depend upon the JSON sent by the server.

The server will decide UI components, I've actually created extended classes for the basic components like UILabel,UITextField etc, but this seems like very lengthy and complex process.

So now I am looking frameworks which can capable to do it. Since I am going to implement it in other apps too, it needs to be generic. Is there any other way to to do with it?

Upvotes: 2

Views: 140

Answers (1)

viral
viral

Reputation: 4208

You can try it out yourself, which would be simpler to implement and debug in case you have any problem. Using any already built framework/library will not provide you flexibility that you MIGHT need.

Consider that you have a function which parses the JSON and decide whether it's textfield/button/label/textview and so on... (It can be one of the attribute in fields array in response).

Make a custom model class say Field, in that class you can have all the details related to the field to be put on the screen; like X, Y, Width, Height, Numeric, Alphanumeric and so on... This all values needs to be parsed from JSON response that you get from API.

You can iterate them one by one like in following function:

- (void)setFieldOnScreen:(Field *)f { // Field is a model class that suits your requirement

    if (f.type isEqualToString:@"UITextField"]) {

        float x = f.x.floatValue;
        float y = f.y.floatValue;
        float width = f.width.floatValue;
        float height = f.height.floatValue;        

        UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
        txtField.delegate = self;
        txtField.font = f.font_name; // from repsonse
        txtField.tag = f.tag.integerValue; // from response
        txtField.textAlignment = f.alignment //NSTextAlignmentLeft or whatever from response

        // even you can fill up preset values in it from response
        txtField.text = f.presetvalue;

            ... and so on....

        } else if ... /// for UILabel/UIButton/UISwitch or any other subclass of the controls
    }
}   

Upvotes: 3

Related Questions