Search code examples
javascriptappceleratorappcelerator-titaniumappcelerator-alloy

Accessing Form Elements in a view from a different Controller


Trying to update the label text in a view from my main controller.

I have 1 controller: index.js Then i have 2 views: index.xml (for iOS) and android_index.xml (for android).

In the view that is linked to the controller I can access form elements like this in my index.js file

$.formLabel.text = "updated text"

but the other view, I store the controller in a variable like. This view is basically an orphan because it doesn't have a controller, I just right clicked and created a new view without a controller.

var win=Alloy.createController('android_index').getView();

and I can't seem to access the form elements in my index.js with:

win.formLabel.text = "updated text"

when I try it says it is undefined

Any help with this issue would be greatly appreciated!


Solution

  • The getView() method return the first top-level view of your controller : https://docs.appcelerator.com/platform/latest/#!/api/Alloy.Controller-method-getView

    If you want access to a child view you can do this :

    var win=Alloy.createController('android_index'); //reference to your controller
    win.formLabel.text = "updated text";
    

    or this works too :

    win.getView('formLabel').text = "updated text";