Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Dynamic view change title

Okay this is confusing me…
I have a database that I create a bunch of buttons and it is done in a while loop.

I create buttons that open another view for a customized selection. When a selection is chosen it would then change the title of the button that opened it. However since there are multiple buttons with the same name it only changes the last button's title.

Is there a way to assign a dynamic name to a view?
Thanks!

— asked December 4th 2010 by Stan Thompson
1 Comment
  • this is confusing.. can u post some code, here or in pastie.org

    — commented December 4th 2010 by Aaron Saunders

9 Answers

  • Accepted Answer

    if you are adding those buttons to view, like you have in that last post, there's a couple things you could do.

    you could create the buttons add them to your view and then push those buttons to an array ( outside of the for loop )

    or you could try referencing it something like this:

    view.children[whichone].title = "new title here";
    
    — answered December 7th 2010 by danno watts
    permalink
    0 Comments
  • Stan

    This doesn't sound like a difficult problem but, I agree with Aaron - no-one can really help you without seeing some code.

    Also, please always include your target mobile platform and Titanium SDK version in your question. The easiest way is to put this information in the question tags.

    Lastly, always set some tags!

    Thanks

    — answered December 4th 2010 by Paul Dowsett
    permalink
    0 Comments
  • I was afraid you were going to say that.
    Okay here it is…
    http://pastie.org/1347540

    — answered December 4th 2010 by Stan Thompson
    permalink
    0 Comments
  • Hi again, Stan

    There are a couple of problems, which probably a good js validator would have helped you to solve. Also, indenting your code properly would make problems more obvious (although admittedly the lack of formatting may have been introduced by pastie).

    I've updated the code, and annotated the issues. Unfortunately, you have left some things out (ie toppos, bigShadenew) meaning that I cannot test it myself. It really helps when you ask questions to post a working, tested script. If you minimise it, by taking out all the non-essential stuff, then that would be ideal.

    //I LEFT OUT A BUNCH OF CODE THAT DOESN'T PERTAIN TO THIS QUESTION
    var rows = db.execute('SELECT * FROM BLAH...BLAH...BLAH');  // <----  problem - was missing closing quote
    
    
    while (rows.isValidRow())
    {
        //CREATES BUTTON THAT I WANT TO CHANGE THE TITLE OF
            // -  NOTE THERE MAY BE MULTIPLES OD THIS WITH THE SAME NAME
    
        var selectionbutton = Titanium.UI.createButton({
            color:'#000',
            top:toppos,
            width:150,
            height:40,
            left: 10,
            title:rows.fieldByName('fieldname')+' [Choose]'
        });
        view.add(selectionbutton);
    
        selectionbutton.addEventListener('click', function(e){
            //OPENS NEW VIEW WITH PICKER
            var viewboxb = Titanium.UI.createView({
                top: 15,
                height:270,
                width:260,
                borderRadius:10,
                backgroundColor:'#333333',
                borderWidth:2,
                borderColor:'#fff'
            });
    
            //OPTIONS HERE
            var okAlertb = Titanium.UI.createButton({
                title:'Okay',
                bottom:10,
                height:35,
                left: 10,
                width:100
            });
    
            viewboxb.add(okAlertb);
    
            okAlertb.addEventListener('click', function(e){
                //////////////////////////////////////////////////
                //HERE IS THE PROBLEM
                //THERE ARE MULTIPLE BUTTONS THIS ONLY CHANGES THE LAST ONE
                //////////////////////////////////////////////////
                selectionbutton.title = theselection;
    //        }); <----  possible problem - this shouldn't be here
                bigShadenew.close(); // <----  shouldn't this be viewboxb.close(); ?
            });
    
            viewboxb.open();
        });
    }
    

    Disregarding the issues I've noted, I can't see any specific problem with the logic of your script.

    Try this version, and let me know how you get on. :)

    — answered December 4th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Hi Hal,
    Thanks for looking at that. However, I wasn't giving working code, I was giving an example of what I was doing so someone could understand what I was doing and give me an idea of how to do it.
    The corrections weren't necessary as the code is working the way it is suppose to.
    What I need to know is… is there a way to name a dynamic view that is being created from a while loop with a different name for each view created?

    So if I have 10 buttons that are all called "selectionbutton" then I tell another view to change the title of the button it will change the right one and not change the 10th one.

    So is there a way to label it or something to identify it later?
    Thanks,

    Stan

    — answered December 4th 2010 by Stan Thompson
    permalink
    0 Comments
  • (just a quick disclosure : i'm typing this from my laptop while in between a photo shoot, so if i have any typos, i'm doing this all from memory )

    let's say you have your 10 buttons, named 'selectionbutton', put into a view named 'theView'. you would access them by using the children method… shown here:

    theView.children[0].title = "new title";
    theView.children[3].title = "new title";
    theView.children[8].title = "new title";
    

    and in case you have a scrollView, then a view added to that scrollView AND THEN you have your buttons added to the view, you would access it like this:

    theView.children[0].children[0].title = "new title";
    theView.children[0].children[3].title = "new title";
    theView.children[0].children[8].title = "new title";
    

    and if you want to identify a specific button you could create a custom variable that holds that specific button's id reference number in, which you would do at creation of the button.

    var scrollView = Titanium.UI.createScrollView({
        contentWidth:320,
        contentHeight:'auto',
        layout:'vertical',
        top:0,
        showVerticalScrollIndicator:true,
        showHorizontalScrollIndicator:false
    });
    
    
    for (var x=0; x<15; x++){
        var button = Titanium.UI.createButton({
            title:'normal title',
            top:10,
            height:35,
            left: 10,
            width:100,
            myID: x
        });
    
        button.addEventListener('click', function(e){
            Ti.API.info('you tapped button number ' + e.source.myID);
    
            // you can change this button's info if you'd like right now
            //e.source.title = 'i've been tapped';    
    
            //or you can access it by the button's parent attribute
            //e.source.parent.children[e.source.myID].title = 'parents made me change my title';
    
            //or you can pass the myID variable to a function that will change it outside of the loop itself
            //changeButtonTitle(e.source.myID);
        });
    
        scrollView.add(button);
    
    
    }
    
    // in case you use the function
    function changeButtonTitle(buttonToChange){
        scrollView.children[buttonToChange].title = 'function changed my title!';
    }
    

    hope that sheds some light on the many different ways to accomplish the same thing!

    rocksteady,
    danno~

    — answered December 5th 2010 by danno watts
    permalink
    0 Comments
  • that help ya?

    — answered December 6th 2010 by danno watts
    permalink
    0 Comments
  • Hi Danno,
    Thanks for the help! Not quite what I was looking for but that might get me in the right direction. Or maybe I just don't understand javascript well enough.
    I believe I am doing the opposite of this:

    e.source.parent.children[e.source.myID].title = 'parents made me change my title';
    

    The child's is changing the parents title.

    I am trying to change the original button title that opened a view and on that view a button's eventlistener will change the original button's title.

    So [BUTTON A] (note: there will be multiple "BUTTON A"s)
    Opens view, the user makes a choice and clicks [OKAY] button.
    The Okay eventlistener saves the choice, closes the view and changes the title of the correct [BUTTON A]. So is it possible to identify the [BUTTON A] based on it's myId value somehow? Or is it possible to give [BUTTON A] a dynamically unique name?
    I know this doesn't work but is here something like this?

    
    i++;
    
    var selectionbutton[i] = Titanium.UI.createButton({
    
    — answered December 6th 2010 by Stan Thompson
    permalink
    0 Comments
  • I found that I can do this

    
    i++;
          var selectionbutton= [];
    
        selectionbutton[i] = Titanium.UI.createButton({
        color:'#000',
        top:toppos,
        width:150,
        height:40,
        left: 10,
        myID: i,
        font:{fontSize:12,fontFamily:'Helvetica Neue'},
        title:rows.fieldByName('fieldname')+' [Choose]'
        });
        view.add(selectionbutton[i]);
    
     selectionbutton[i].addEventListener('click', function(e)
        {
    
    
    whichone= e.source.myID;
    

    However, when I go to change it it still does not work.

    
    selectionbutton[whichone].title = 'blah blah';
    

    It works if I click on the last button but others are undefined.

    — answered December 6th 2010 by Stan Thompson
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.