当前位置: 动力学知识库 > 问答 > 编程问答 >

Appcelerator Titanium Alloy | How to access element of other class

问题描述:

In Alloy Titanium, I can access XML elements with their id $.element_id but how can I get elements of other class? or xml?

EDIT

I have two files.

1. file1.js, file1xml.xml

2. file2.js, file2xml.xml

In File1.js i want to access the variable of file2xml.xml. how can i achieve this?

网友答案:

Anything with an id can be accessed:

file1.xml

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

file2.js

var ctrl1 = Alloy.createController('file1');
ctrl1.myView.backgroundColor = 'red';
网友答案:

You can use Require tag in alloy with an Id and you can access its elements as below.

**File1.xml**

<Alloy>
<View>
    <Label id="labelId">Middle</Label>
</View></Alloy>


**File2.xml**

<Window>
    <Require src="File1" id="File1View" type="View"/>
    <View id="header"><Label id="headerText">Header</Label></View>
    <View id="nav">
        <Label class="navButton" onClick="openTab" controllerName="home">Home</Label>
        <Label class="navButton" onClick="openTab" controllerName="news">News</Label>
        <Label class="navButton" onClick="openTab" controllerName="info">Info</Label>
        <Label class="navButton" onClick="openTab" controllerName="more">More</Label>
    </View>
</Window> 

**File2.js**

$.File1View.labelId.text = "hi";
网友答案:

if you have required file2.xml in file1.xml like

<Require src="common/viewToolBar" id="viewToolBar"/>

then you can get the element with the id in file1.js like

$. viewToolBar.getView('viewSynch').visible = false;

link for more details

分享给朋友:
您可能感兴趣的文章:
随机阅读: