Search code examples
javascripttitaniumappcelerator

How to create a button through the press of another button in Titanium Appcelerator?


My question was phrased horribly, I'm sorry.

So what I wanna do is to go into the createNewCourseScreen, enter the name and set the color and press the create button, and the button created will pop up under the other buttons in the coursesScreen.

I'm a meganoob in titanium and need your help!

Screens coursesScreen: http://s30.postimg.org/do1jrlrrl/courses.png newCourseScreen: http://s29.postimg.org/7c15cxnon/newcourse.png


Solution

  • You may be over complicating the thought process on this. Maybe you are using Alloy, which is less direct on seeing how to accomplish this. In classic Titanium, you'd already have the code showing how to create the button and the event listener staring at you.

    General Steps: 1 - Get inputs for the title and the color. 2 - Call the onClick or addEventListener('click', ... handler for the create button. 3 - In the function that handles this event, create a button with Ti.UI.createButton. 4 - Create an event listener for it. 5 - Add the button to the screen where you want it to display.

    classic:

    // Using the same logic that created the other buttons
    function createButtonPress(){
        // Store information about new button created somewhere so that it
        // shows up the next time the application is run.
    
        // Create the button.
        var newButton = Ti.UI.createButton({
            title: nameFromTextField.value
            color: colorFromTextField.value
        });
        newButton.addEventListener('click', function(){
            // Thing you want the new button to do when pressed.
        });
        viewHoldingButtons.add(newButton);
    }
    

    Alloy:

      function createButtonPress(){
          // Store information about new button created somewhere so that it
          // shows up the next time the application is run.
    
          // Create the button.
          var newButton = Ti.UI.createButton({
              title: nameFromTextField.value
              color: colorFromTextField.value
          });
          newButton.addEventListener('click', function(){
              // Thing you want the new button to do when pressed.
          });
          $.viewHoldingButtons.add(newButton);
       }