In Appcelerator Titanium I'm creating a label which initially is set to some text. I then add a button which when clicked calls the scanController module, and that module needs to change that text. So within scanController I'm calling the setResultTxt()
method in the scanView module you see below. But when do that it says that myResultTxt
is null! Why is that?
I'm still using Titanium SDK 1.7.5 because I have trouble upgrading to a newer version.
This is a complete working example of the problem:
app.js
var win = Titanium.UI.createWindow({
backgroundColor:'#fff',
layout:"vertical"
});
win.open();
var module = require('scanView');
module.createScanPage(win);
scanView.js
var myResultTxt, theButton;
exports.createScanPage = function(theWindow) {
myResultTxt = Ti.UI.createLabel({
text: 'Some initial text',
top:40,
});
theWindow.add(myResultTxt);
theButton = Ti.UI.createButton({
title: 'Do something',
top:20
});
theButton.addEventListener('click', function() {
alert('clicked');
var b = require('scanController');
b.startScanning();
});
theWindow.add(theButton);
};
exports.setResultText = function(str) {
myResultTxt.text = str;
};
scanController.js
exports.startScanning = function() {
var a = require('scanView');
a.setResultText('My new text');
};