Search code examples
objective-ciosjsongeneric-programmingquickdialog

generic class for quickDialog forms


Let me first start with explaining the situation. I am making a IOS app that is working with a webservice and a webApplication. Take the following example.

Let's say we have company X. company X can make all kinds of forms with the webapplication (Callback form, meetingRequest form, vacationRequestForm,...) Employers will then open their application and see all the titles of these forms in a tableview. When they click on a title they go to the next screen and see the entire form and can start filling it up.

The difficulty here is that each company can create their own forms. So company X maybe have other kinds of forms than company Y. After a lot of research I found the amazing library of QuickDialog. You can find it over here. Definitely worth it to check it out!

PROBLEM Okay now the problem. Offcourse the data should be send back to the server once a user filled up the form. To do that I am working with a controller class and a NSObject class. The best way to understand is look at the login example. First of all, this is the json of the login.

{
    "grouped": true,
    "title": "Login Form",
    "controllerName": "LoginController",
    "sections": [
        { "title":"login", "elements": [
                { "type":"QEntryElement", "title":"Login","placeholder":"Login or email", "bind":"textValue:login", "key":"login"},
                {  "type":"QEntryElement", "title":"Password", "placeholder":"Password",  "secureTextEntry":true, "bind":"textValue:password" }
            ]
        },
        { "elements":[
                { "type":"QButtonElement", "key":"button", "title":"Login", "controllerAction":"onLogin:"}
            ]
        }
    ]
}

You can see that the two fields have a binding option. The project contains a NSObject class (loginInfo) In this class those fields are added as NSStrings.

@interface LoginInfo : NSObject {
@private
    NSString *_password;
    NSString *_login;
} 
@property(strong) NSString *login;
@property(strong) NSString *password;
@end

Next there is a controller class. In this controller class you say that when the button is pushed you fetch the values from the textfields into this loginInfo class. This is done at the following way.

- (void)onLogin:(QButtonElement *)buttonElement {

    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    [self loading:YES];
    LoginInfo *info = [[LoginInfo alloc] init];
    [self.root fetchValueUsingBindingsIntoObject:info];
    [self performSelector:@selector(loginCompleted:) withObject:info afterDelay:2];
}
- (void)loginCompleted:(LoginInfo *)info {
    [self loading:NO];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:[NSString stringWithFormat: @"Hi %@, I hope you're loving QuickDialog! Here's your pass: %@", info.login, info.password] delegate:self cancelButtonTitle:@"YES!" otherButtonTitles:nil];
    [alert show];
}

Okay I know this is alot of info. But I only want a good explanation of my problem. I think the controller class is not my problem but the NSObject class is. I want this to be generic. Because this should be filled up with the properties it gets back from the json. I don't know how I can do that.

I hope anybody can help me! If you need more explanation feel free to ask!

Kind regards and many thanks in advance !


Solution

  • fetchValueUsingBindingIntoObjects: takes an "id", which means pretty much any object that supports KVC will do it:

    NSMutableDictionary *dict = [NSMutableDictionary new];
    [self.root fetchValueUsingBindingsIntoObject:dict];
    

    This way you can get data about any form, and use it to send it back to the web.