Dashcode: Changing a simple Text Label

Discussion in 'Mac Programming' started by Halloween, Jul 14, 2008.

  1. Halloween macrumors newbie

    Joined:
    Jul 14, 2008
    #1
    Hey guys, what's up? First post here (although i've been visiting MacRumors for the past years).

    I've recently downloaded XCode and begun messing around with it a couple of days ago, but I'm afraid I've been having some problems with even a simple "change the text label" funcionality (if we could call it that lol).
    More specifically, I want to try out a very basic operation of "Click this button to change that label's text to XYZ". Now, the tutorials at developer.apple.com haven't been particularly helpful. In fact, I am quite surprised to see that such a trivial thing does not have a tutorial for it or something like it.

    Most documentation I've seen are usually either too simplified (e.g.: no code required) or not intuitive at all (damn, I miss PHP already lol), so I'd like to hear what you have to say.

    Anyway, to the code - this is in the main js file, called iTSLS.js in this case.

    I have a button (id: button) - which has an event handler which trigger the "mudar_texto" (translating: change text) function.
    The code is:

    Code:
    function mudar_texto(event)
    {
       if (window.widget)
       {
    	// Values you provide
    	var textFieldToChange = document.getElementById("showStats");	// replace with ID of text field
    	var newTextFieldText = "FFS";						// value to change range to
    
    	// Text field code
    	textFieldToChange.value = newTextFieldText;
       }
    }
    
    Now, the text label which I want to change has the correct ID ("showStats") and, as you can see, the code was even copied and pasted directly from the code snippet library. I'm just wondering why on earth won't this work.

    Thanks in advance guys,
    Hal
     
  2. listylister macrumors newbie

    Joined:
    Mar 29, 2008
    #2
    Have you checked to see if the If Statement is getting executed?

    I have had this problem many times. Where the If Statement is False and the code inside of it does not get parsed - Just a Suggestion.
     
  3. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
    #3
    Yes, it is. The "if (window.widget)" - if I understood it correctly from the docs - will execute something only if the widget is running on Dashboard. Either way, I have tested it both with and without it; it's not that, but thanks for the suggestion.
     
  4. listylister macrumors newbie

    Joined:
    Mar 29, 2008
    #4
    Could you post the other bit of code that shows the ID of the text field. And remember to wrap it in CODE tags
     
  5. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
    #5
    Hmmm... where?
    Here's a printscreen of that ID:
     

    Attached Files:

  6. listylister macrumors newbie

    Joined:
    Mar 29, 2008
    #6
    Your id's do not match up...

    The one on your attached image shows that the ID is 'showText' whilst the one in your code shows 'showStats'. You will need to match these up so that it can actually pinpoint which control you are referring to.
     
  7. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
    #7
    My bad, sorry.
    I tried to delete and create a new text label today, so I messed up the name; either way, I had the code updated to showText as well - just forgot to bring you up to speed on this.

    Still, i doublechecked and swapped everything to 'showStats'...
    Anyway, still not working :/
     

    Attached Files:

  8. listylister macrumors newbie

    Joined:
    Mar 29, 2008
    #8
    Ok i just tried setting the value of a label myself and it also failed.

    Have you tried something like setValue or experimenting with different functions. I am no expert on this so what I would do is just play around with different techniques.

    If someone else has anything to add please feel free :)
     
  9. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
    #9
    Ok, I just tried using:

    Code:
     
          var a = widget.getElementById("showStats").value;
          var newTextFieldText = "FFS";
          
          a.value = newTextFieldText;
    
    but it's still not working :confused: it just errors with "Value undefined (result of expression widget.getElementById) is not object"

    If anyone manage to get some wizardry working, please let me know.

    PS: Is there a way to keep the damn error log always on top of that Stack Frame view? Really bothers me that it can't be done (apparently)
    PS2: And dashcode should allow us to copy/paste the error messages as well....
     
  10. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
  11. rchnashville macrumors newbie

    Joined:
    Jul 31, 2008
    #11
    Try this :)

    document.getElementById("YourTextId").innerText = "New Value";

    I hope that helps!
    Ryan
     
  12. Halloween thread starter macrumors newbie

    Joined:
    Jul 14, 2008
    #12
    Perfect! Thanks a million :D
    Still, funny how such a simple thing was so hard to spot
     

Share This Page