Nativescript - Как получить тип ребенка - PullRequest
0 голосов
/ 24 октября 2018

Кто-нибудь знает, чтобы определить, 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, как указано выше.И когда я нажимаю кнопку, мне нужно знать тип View каждого ребенка.

Кто-нибудь может помочь?

1 Ответ

0 голосов
/ 24 октября 2018

Попробуйте эту ссылку, она показана здесь:

typeName

...

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

  }
...
...