Nativescript - 如何获取 Child 的类型
Nativescript - How to get the Type of Child
有谁知道判断child
是TextView
还是Image
?
<template>
<Page>
<StackLayout>
<StackLayout ref="container">
</StackLayout>
<Button text="Get Child!" @tap="getChild" />
</StackLayout>
</Page>
</template>
<script>
const Image = require("tns-core-modules/ui/image").Image;
const TextView = require("tns-core-modules/ui/text-view").TextView;
export default {
mounted() {
let container = this.$refs.container.nativeView;
let tv = new TextView();
let img = new Image();
container.addChild(tv)
container.addChild(img)
},
methods: {
getChild() {
let container = this.$refs.container.nativeView;
let childLength = container.getChildrenCount(); // return 2
for(let i=0; i<childLength; i++) {
let child = container.getChildAt(i)
// How to determine the child is <Image/> or <TextView/> here?
// What I got is an Object, and I can see nothing related to it..
}
}
}
}
</script>
在我的例子中,我将动态地将 child
附加到 container
中,如上所述。当我按下按钮时,我需要知道每个 child 的 View
类型。
有人能帮忙吗?
试试这个 link,它显示在这里:
...
for(let i=0; i<childLength; i++) {
let child = container.getChildAt(i)
// Determine the child
if(child.typeName == 'TextView') // this is TextView
else if(child.typeName == 'Image') // this is Image
}
...
有谁知道判断child
是TextView
还是Image
?
<template>
<Page>
<StackLayout>
<StackLayout ref="container">
</StackLayout>
<Button text="Get Child!" @tap="getChild" />
</StackLayout>
</Page>
</template>
<script>
const Image = require("tns-core-modules/ui/image").Image;
const TextView = require("tns-core-modules/ui/text-view").TextView;
export default {
mounted() {
let container = this.$refs.container.nativeView;
let tv = new TextView();
let img = new Image();
container.addChild(tv)
container.addChild(img)
},
methods: {
getChild() {
let container = this.$refs.container.nativeView;
let childLength = container.getChildrenCount(); // return 2
for(let i=0; i<childLength; i++) {
let child = container.getChildAt(i)
// How to determine the child is <Image/> or <TextView/> here?
// What I got is an Object, and I can see nothing related to it..
}
}
}
}
</script>
在我的例子中,我将动态地将 child
附加到 container
中,如上所述。当我按下按钮时,我需要知道每个 child 的 View
类型。
有人能帮忙吗?
试试这个 link,它显示在这里:
...
for(let i=0; i<childLength; i++) {
let child = container.getChildAt(i)
// Determine the child
if(child.typeName == 'TextView') // this is TextView
else if(child.typeName == 'Image') // this is Image
}
...