Search code examples
iostitaniumtitanium-mobiletitanium-modules

TiViewProxy and view creation


I'm working on a titanium module.

Little bit confused about the TiViewProxy.

Why we are using -(void)setColor_:(id)color such methods (_ in methods) in ViewProxy ?

If we didn't write any such methods what happens when we call:

myModule.createView({
  color : 'red'
});

If I didn't passed any argument on my createView() method how my view creation code will work on my module.

Means:

I'm just creating my view in my app.js like:

myModule.createView();

I'm handling the view creation code inside:

-(void)setColor_:(id)color
{
}

If I'm not passing any argument how my view is created ? Will it work ?


Solution

  • I'll explain the various moving parts. It's all part of the platform, so not critical that you know how it works. Knowing that it does this for you is important, though. So...

    When you call myModule.createView(), the platform looks for a child of your module that matches certain constraints. Let me expand that statement by looking at the ti.pageflip module. myModule's class is TiPageflipModule. When I call myModule.createView(), the platform will look for TiPageflipViewProxy so it can instantiate it. TiPageflip comes from the module's name, minus "Module". "View" comes from createView. "Proxy" is tagged on because that's how we get from JavaScript to native. The TiPageflipViewProxy creates a native view, TiPageflipView. The proxy handles interactions between JavaScript and the native view. Make sense so far?

    Part of the naming convention for exposed properties is that they are suffixed with an _. The platform looks for these methods, and calls each of them set in the creation dictionary createView({ whatever: 'value' }) as well as the property view.whatever = 'value'; or the method view.setWhatever('value'). (Search for the word "underscore" in the iOS mod dev guide, it only occurs once, to read more: https://wiki.appcelerator.org/display/guides/iOS+Module+Development+Guide).

    Because you aren't defining createView, the platform is doing it for you, and it handles createView() just the same as createView({}). It's an optional param. That's just by definition.