Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nib resolver for view controllers #82

Closed
eriksundin opened this issue Sep 14, 2013 · 7 comments
Closed

Nib resolver for view controllers #82

eriksundin opened this issue Sep 14, 2013 · 7 comments

Comments

@eriksundin
Copy link
Contributor

Here's a suggestion for a default factory post processor to include for iOS:

TyphoonViewControllerNibResolver

  • Looks for definitions that creates a UIViewController
  • Sets the -initWithNibName:bundle: selector and injects a nib name
  • Defaults to resolving a nib with the same name as the class, user can sub-class to implement custom logic.

Example:

- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory
{
    for (TyphoonDefinition *definition in [factory registry])
    {
        if ([definition.type isSubclassOfClass:[UIViewController class]])
        {
            [self processViewControllerDefinition:definition];
        }
    }
}

- (void)processViewControllerDefinition:(TyphoonDefinition *)definition
{
    TyphoonInitializer *initializer = definition.initializer;

    if (initializer.selector == nil || ([self isInitWithNibNameBundle:initializer.selector] && [initializer.injectedParameters count] == 0))  {

        initializer.selector = @selector(initWithNibName:bundle:);
        [initializer injectWithValueAsText:[self resolveNibNameForClass:definition.type] requiredTypeOrNil:[NSString class]];
        [initializer injectWithObject:[NSBundle mainBundle]];

    }
}

- (BOOL)isInitWithNibNameBundle:(SEL)sel {
    return [NSStringFromSelector(sel) isEqualToString:NSStringFromSelector(@selector(initWithNibName:bundle:))];
}


- (NSString *)resolveNibNameForClass:(Class)viewControllerClass
{
    return NSStringFromClass(viewControllerClass);
}
@eriksundin
Copy link
Contributor Author

Probably a good idea to also add an 'exclusions' property if you want the processor to skip some component.

@jasperblues
Copy link
Member

Hi Erik,

This looks cool . . we could also implement 'name-space handlers'. This way you could have:

<mvc:controller nib-name="myNib">          
  <property name="" ref=""/>
</mvc:controller>
  • Providing a nib-name will assume the use of initWithNibName:bundle
  • Could also have a has-nib = yes to default to same nib-name as controller

There are other nice things you could put in the mvc namespace. . . we'd need something similar for the block-style, I guess.

To proceed with this, we need to:

  • Create a folder for iOS specific stuff. And for OSX.
  • Update the .podspec so that it handles this.

ps: Personally, I tend to favor UIViews implmented in code! (Not that there's anything wrong with xibs of course - wide range of tastes here).

@eriksundin
Copy link
Contributor Author

I like the MVC name-space idea. The block-style syntax could follow the same pattern.
Ex.

[TyphoonDefinition withViewControllerClass:[MyViewController class] nibNameOrNil:@"MyViewControllerView" properties: … ];

How ever it differs a bit from what I try to achieve with the postProcessor. Create a new issue for the MVC name-space support perhaps?

Myself, I tend to use xibs for most view controllers. And there is a lot of boiler-plate code defining these in the Assembly currently. In one current project I am working on we are evaluating which xib to load in runtime, depending on if the device is an iPhone4 / iPhone5. With the postProcessor as an optional component that logic could easily be implemented in a subclass of TyphoonViewControllerNibResolver.

@eriksundin
Copy link
Contributor Author

Hmm. I guess I could always just implement this class myself and attach it as a postProcessor in the project.
The question is if it's a common enough use-case to include the component in Typhoon I guess.

@jasperblues
Copy link
Member

You can go ahead and push it to Typhoon if you like (you have push access). I think it will be useful to others.

The one prerequisite is to:

a) Put it in an iOS folder
b) update the podspec so that only iOS targets get this.

This way it won't break OSX.

@eriksundin
Copy link
Contributor Author

SHA: b32e214

@jasperblues
Copy link
Member

Nice work Erik!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants