Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2014
    Location
    Texas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Can't Figure This Out in Titanium

    Hey everyone! This is my first post and I am new to programming. I'm currently in school for Mobile Development. We are using Titanium to do our coding. What I have done is created some simple windows, views and labels. Using the iOS Simulator I am using a main view with text in it, then below is a Next and Previous button. I am trying to cycle through an array of my 5 favorite sports. So basically, when I click Next, it goes from one sport, to another and so on. Same with Previous. It just isn't working properly and I can't figure it out.

    Here is the code:

    PHP Code:
    var 0;

    var 
    nextSports = function(){
        
        
    welcomeView.hide();
        
        if(
    sports.length){
        
            
    sportsText.text sports[i]; //text is set first
            
    i++;//then i is increased
               
            
    console.log(i);
                
        } else {
            
            
    0;
            
    sportsText.text sports[i];  
            
    i++; 
            
    console.log(i);
        };
        
        
    originWindow.add(sportsText);
        
    };




    // Previous
    var previousSports = function(){
        
        
    welcomeView.hide();
        
        if(
    === 0){ 
            
            
            
    sportsText.text sports[i]; // I set the text first, just like above.
            
    sports.length 1// then i is decreased
             
            
    console.log(i);
             
            
                
        } else {
            
            
    sportsText.text sports[i];  // I set the text first, just like above.
            
    i--;   // then i is decreased
            
    console.log(i);
        };
        
        
    originWindow.add(sportsText);
        
    };






    // Next Button Label
    var sportsText Ti.UI.createLabel({
            
    textsports[i],
            
    color"#000",
            
    textAlign"center",
            
    height150,
            
    top120//changed top just to see if I can see the label.
            
    left20,
            
    right20,
            
    backgroundColor"#fff",
            
    borderRadius5,
            
    borderWidth1,
            
    borderColor"green"
            
        
    });    





    // Event Listeners 
    nextButtonView.addEventListener("click"nextSports);
    previousButtonView.addEventListener("click"previousSports); 



    Also, this is the code for the array, window, views, etc.. so you can see my whole application.


    PHP Code:
    Ti.UI.setBackgroundColor("#000");

    var 
    sports = ["Baseball""Soccer""Basketball""Football""Hockey"];


    var 
    originWindow Ti.UI.createWindow({
        
    //Reserved Properties
        
    backgroundColor"#848484"
        
    });


    var 
    welcomeView Ti.UI.createView({
        
        
    backgroundColor"#fff",
        
    borderRadius5,
        
    borderWidth1,
        
    height150,
        
    top120,
        
    left20,
        
    right20,
        
    borderColor"green"
        
    });


    var 
    welcomeText Ti.UI.createLabel({
        
        
    text"To View My Favorite Sports\n\nClick Below",
        
    color"#000",
        
    font: {fontize14fontFamily"Helvetica"fontWeight"bold"fontStyle"italic"},
        
    textAlign"center",
        
    top25,
        
    left25,
        
    right25,
        
    bottom25
        
    });



    var 
    nextButtonView Ti.UI.createView({
        
        
    backgroundColor"#23ba00",
        
    topwelcomeView.top welcomeView.height 50,
        
    left180,
        
    right20,
        
    borderRadius2,
        
    height50,
        
    borderWidth1
        
    });

    var 
    nextButtonText Ti.UI.createLabel({
        
        
    color"#fff",
        
    font: {fontSize16fontFamily"Helvetica"fontWeight"bold"},
        
    textAlign'center',
        
    text"Next"
        
    });



    var 
    previousButtonView Ti.UI.createView({
        
        
    backgroundColor"#23ba00",
        
    topwelcomeView.top welcomeView.height 50,
        
    left20,
        
    right180,
        
    borderRadius2,
        
    height50,
        
    borderWidth1
        
    });

    var 
    previousButtonText Ti.UI.createLabel({
        
        
    color"#fff",
        
    font: {fontSize16fontFamily"Helvetica"fontWeight"bold"},
        
    textAlign'center',
        
    text"Previous"
        
    });


    var 
    loadFile = require("buttons");

    originWindow.add(welcomeViewnextButtonViewpreviousButtonView); 
    welcomeView.add(welcomeText);
    nextButtonView.add(nextButtonText);
    previousButtonView.add(previousButtonText);
    originWindow.open(); 


    I'm sorry about such a long post. I've been talking to my instructor for over a day now, trying to figure out what the problem is, so I can fully understand it. Every comments he gives me doesn't seem to solve the problem. You are not helping me cheat or anything like that. This was a project turned in last Saturday. I just want to understand this before moving on.

    Thanks,
    Jack

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't know anything about titanium, but to me it looks like this line:
    Code:
     i = sports.length - 1; // then i is decreased
    is a little suspect. Without knowing much else, it would make more sense if it was
    Code:
    i = i - 1;
    or even just
    Code:
    i--;
    maybe you could be a little more specific about your problem - "it just isn't working properly" doesn't help much. What do you expect it to do, and what does it do instead?

  • #3
    New to the CF scene
    Join Date
    May 2014
    Location
    Texas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply!

    Unfortunately, it didn't work. Thanks though!

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    ah, yeah - I see now what that line is for. Well, like I say, if you can't describe the problem any more, it's going to be tough to help you.

  • #5
    New to the CF scene
    Join Date
    May 2014
    Location
    Texas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ill see if I can explain it better. When I click the Next button on the iOS simulator, it cycles through all my sports(Baseball, Soccer, Basketball, Football, Hockey) like it's supposed too. Let's say I click Next and Soccer appears on the screen. If I click Previous, it shows Basketball. If I click Previous again, then it goes back to Soccer, then Baseball then screen goes blank.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    you can simplify your functions and replace the if/else routine with a ternary operator...

    Code:
    var nextSports = function(){ 
    
    	welcomeView.hide(); 
    	
    	i = i <sports.length-1 ? i+1 : 0;    
    	
    	sportsText.text = sports[i];   
    	
    	originWindow.add(sportsText); 
        
    }; 
    
    
    
    
    // Previous 
    var previousSports = function(){ 
         
       	welcomeView.hide(); 
    	
    	i = i > 0 ? i-1 : sports.length-1;   
    	
    	sportsText.text = sports[i];   
    	
    	originWindow.add(sportsText);
         
    };

  • #7
    New to the CF scene
    Join Date
    May 2014
    Location
    Texas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    you can simplify your functions and replace the if/else routine with a ternary operator...

    Code:
    var nextSports = function(){ 
    
    	welcomeView.hide(); 
    	
    	i = i <sports.length-1 ? i+1 : 0;    
    	
    	sportsText.text = sports[i];   
    	
    	originWindow.add(sportsText); 
        
    }; 
    
    
    
    
    // Previous 
    var previousSports = function(){ 
         
       	welcomeView.hide(); 
    	
    	i = i > 0 ? i-1 : sports.length-1;   
    	
    	sportsText.text = sports[i];   
    	
    	originWindow.add(sportsText);
         
    };

    Hey thanks for the code! It works great. I haven't learned what a ternary operator is, so our instructor told me to use the if/else statements. Thanks a lot though!

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it's a handy little shortcut for setting a variable when there can only be two possible outcomes, based on a true/false proposition. Basically, this:
    Code:
    i = i <sports.length-1 ? i+1 : 0;
    is the same as
    Code:
    if(i <sports.length-1){
    i=i+1;
    } else {
    i=0;
    }

  • #9
    New to the CF scene
    Join Date
    May 2014
    Location
    Texas
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Which means:

    PHP Code:
    //Previous Button

    i-sports.length-1


    Is the same as:

    PHP Code:
    if(0){ 
            
            
    i--;
             
        } else {
            
            
    sports.length 1;
        
        }; 


    Am I correct?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •