【问题标题】:How to use UI object inside an included view in Titanium Alloy如何在钛合金的包含视图中使用 UI 对象
【发布时间】:2013-08-28 16:24:33
【问题描述】:

我有四个类似的文件:

index.xml

<Alloy>
    <Window>
        <View class="container">
            <View id="report">

                <!-- Adds a textfield for name entry -->
                <Require type="view" src="textfield" id="name"/>

                <Button id="checkNameValue" title="Check Value"  width="100" height="40" onClick="checkname"/>
            </View>
        </View>
    </Window>
</Alloy>

index.js

function checkname(e) {
    alert("Your name is " + $.name.getView('nameTextField').value);
}

textfield.xml

<Alloy>
    <View id="nameView">
    </View>
</Alloy>

textfield.js

var nameTextField = Titanium.UI.createTextField({
    hintText:"Type your name",
    height:40,
    width:300,
    top:20,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

$.nameView.add(nameTextField);

当我尝试单击按钮读取名称值时,出现错误:

message = "'undefined' is not an object (evaluating $.name.getView(\"nameTextField\").value')";

出了什么问题,我该如何解决?

【问题讨论】:

  • 你试过在 textfield.js 中使用nameTextField.value 吗?或者你可以使用$.name.children[0].children[0].value

标签: mobile titanium titanium-alloy


【解决方案1】:

尝试将 textfield.js 更改为

$.nameTextField = Titanium.UI.createTextField({
    hintText:"Type your name",
    height:40,
    width:300,
    top:20,
    borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});

$.nameView.add($.nameTextField);

那么你的警报代码应该是这样的

alert("Your name is " + $.name.nameTextField.value);

【讨论】:

    【解决方案2】:

    您只需将您的文本字段添加到包含的文件中,就像

    textfield.xml

    <Alloy>
        <View id="nameView">
              <TextField value="" hintText="Type Here" id="textfield" />
        </View>
    </Alloy>
    

    现在您可以轻松地将文本字段访问到您的索引文件中:

    index.js

    alert($.name.textfield.value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2013-10-31
      • 2015-02-04
      • 1970-01-01
      相关资源
      最近更新 更多