【发布时间】:2013-11-25 07:17:45
【问题描述】:
在 Appcelerator Titanium 中,我正在创建一个最初设置为某些文本的标签。然后我添加一个按钮,单击该按钮会调用 scanController 模块,并且该模块需要更改该文本。所以在 scanController 中,我在下面看到的 scanView 模块中调用setResultTxt() 方法。但是当它说myResultTxt 是空的!这是为什么呢?
我仍在使用 Titanium SDK 1.7.5,因为升级到较新版本时遇到问题。
这是该问题的完整工作示例:
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');
};
【问题讨论】:
-
我使用 Titanium SDK 3.1.4 运行您的代码,并且运行正常。问题出在代码的不同部分,或者是旧 SDK 的问题,最好弄清楚如何更新到最新版本。
标签: javascript titanium titanium-mobile appcelerator-mobile commonjs