xmartlabs / XLForm

XLForm is the most flexible and powerful iOS library to create dynamic table-view forms. Fully compatible with Swift & Obj-C.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

initWithCoder support

wefish opened this issue · comments

I'm using segues on my project. Navigation is performed using code like

[self performSegueWithIdentifier:@"editData" sender:self];

And the data to be edited is asigned to destination view controller on the prepareForSeguemethod

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"editWorkerProfile"])
    {
        UINavigationController* navController = [segue destinationViewController];
        MyEditorViewController *editorController = (MyEditorViewController *) [navController topViewController];
        editorController.data = dataToBeEdited;
    }
}

The destination ViewController is initialized using "initWithCoder" and form structure is created on the "viewDidLoad" (Because data property is assigned after initialization).

With te "1.0.0" version of XLForm I used a "trick" like this on destination ViewController:

- (id)initWithCoder:(NSCoder*)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        XLFormDescriptor * form  = [XLFormDescriptor formDescriptorWithTitle:@"work profile"];
        return [super initWithForm:form]; // Second init... it works with XLFrom 1.0.0!!!
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Form content is built after initialization
    [self buildForm: self.form withData: self.data]; 
}

With the actual git XLForm version this code is not working anymore: form appears empty...

¿What's the correct way to use XLForm with storyboard & segues?

Hey @wefish , you can extend XLFormViewController and configure the form DSL from XLFormViewController subclass, take a look at ExamplesFormViewController class, this VC is used by iPhoneStoryboard.

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard" bundle:nil];
    [self.window setRootViewController:[storyboard instantiateInitialViewController]];

In case you don't want to extend XLFormViewController, you can set up the form DSL (XLFormDescriptor) using @property XLFormDescriptor * form; property,.

I think you can use this property either after the class initialization or from prepareForSegue.


Your [super initWithForm:form] invocation from - (id)initWithCoder:(NSCoder*)aDecoder is wrong, don't do that. This is a helper initializer not intended to be used with storyboards, it calls [super initWithStyle:style].


I've removed awakeFromNib and added initWithCoder to XLFormViewController.

Let me know if this helps.

Thanks Martin, with new initializer initWithCoder all is working ok.

this is my actual code:

- (id)initWithCoder:(NSCoder*)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        XLFormDescriptor * form  = [XLFormDescriptor formDescriptorWithTitle:@"my title"];
        self.form = form;
    }
    return self;
}

form sections and rows are added on "viewDidLoad".

Thank you!!