Stupid UILabel question

Discussion in 'iOS Programming' started by mngoose, Jun 10, 2012.

  1. mngoose macrumors member

    Joined:
    May 26, 2012
    #1
    greetings all,

    After pulling my hair out and wearing out my keyboard searching google and the forum this is driving me nuts.

    I am trying to change the text on a label with a variable that is coming from my JSON data array.

    the variables show up on my output like they are supposed to but I have tried just about everything that I can find to get the label to change. I finally got it to go null and make the box disappear off the view but I cant get it to display the variable and when the output of NSLog is nothing but nul;

    Here is the code
    Code:
    
            tickdatetime.text = datetime;
            NSLog(@"%@", tickdatetime);
    
    It wont take the data that is in datetime. Which I can see the JSON Data in the output window.

    Code:
    
    @synthesize tickdatetime = _datetime;
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.tickdatetime.text = datetime;
        
        
    }
    
    and in the .h file
    
    IBOutlet UILabel *tickdatetime;
    
    

    This is driving me nuts.... It should be a simple set of coding buts its been confusing me .

    John
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You should probably explain what datatype datetime is and provide the code that sets its value.
     
  3. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #3
    Dejo we meet again

    Code:
    @property   (nonatomic, retain) NSString *datetime;
    
    that is in my .h file

    Code:
    datetime =      [my2ConvertedJSONData valueForKey:@"datetime"];
    
    and that is in my .m file during the JSON data parsing into the array.

    If I type
    tickdatetime = datetime;

    I get the proper result into the tickdatetime variable but it still will not place the value into it when I try the tickdatetime.text = datetime code.

    Thanks in advance for the help
    john
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    You have several different things here:

    Code:
    IBOutlet UILabel *tickdatetime;
    
    This is an instance variable. Its name is tickdatetime. It is an IBOutlet.


    Code:
    @synthesize tickdatetime = _datetime;
    
    This is a property. Its name is tickdatetime. It is not an IBOutlet (you can tell because it's not declared as IBOutlet. It is bound to the instance variable named _datetime.

    Why would you make a property with the same name as an instance variable, yet bind it to a different instance variable? That's not a rhetorical question. I would like an explanation of what you're trying to accomplish with this property and two distinct instance variables. If you don't know why, or can't explain what you're trying to accomplish, then you probably need to work on the design. In particular, define exactly what you want to do (write it down), then post that.


    Code:
            tickdatetime.text = datetime;
    
    What do you expect the name tickdatetime to refer to? Do you expect it to refer to the ivar, or to the property? Again, if you don't know, then you should figure out what you want to accomplish and describe it clearly.

    I can tell you that this name is referring to the ivar.

    Code:
        self.tickdatetime.text = datetime;
    
    Same questions here: What do you expect the name tickdatetime to refer to? Do you expect it to refer to the ivar, or to the property?

    I can tell you that this name is referring to the property. Since the property is backed by the ivar named _datetime, not the ivar named tickdatetime, I can assure you that these are two separate and distinct variables, with separate contents. Of these two variables, only one is an IBOutlet.


    I have no idea what you're trying to accomplish. To me, it looks like you've copied and pasted code without understanding it. In particular, this code:
    Code:
    @synthesize tickdatetime = _datetime;
    
    seems completely out of tune with everything else. You don't seem to realize that there's a different ivar backing the property. Nor do you seem to realize that you need IBOutlet on the property.

    Analogy: you have a dog, its name is "Dog". You have a cat, its name is also "Dog". You say, "Sit, dog, sit". Which animal do you expect to respond?

    If you're learning from some book or tutorial, please tell us exactly what it is. If you've completed a book or tutorial, please tell us exactly what it is. If you aren't learning from anything, you probably need to consult a book or tutorial, which will provide more structured guidance. As given, the code is hopelessly confused and confusing.
     
  5. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #5
    Chown33,

    I guess I forgot to post this code from the .h file

    Code:
    @ property (nonatonic, retain) IBOutlet UILabel *tickdatetime;
    
    
    This is connected to the label that is on the second view of the application

    What I am trying to do is bring the JSON Data into the second view. The UILabel is just one of a half dozen on the screen when it runs, and I am only working on one line at a time in order to get it right before working on the next variable.

    the tickdatetime.text = datetime; is the label name and I want to put the datetime information into the label. That is what I am trying to accomplish.

    I understand code structure and am working my way through it . I am primarily a HTML and Android programmer.

    I have done several begining IOS programming and have scoured the internet I am sure it is something simple that I am missing as I know have been able to change the label text by doing this

    Code:
     }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.tickdatetime.text = @"this is a test of the label";
        
        
        
    }
    
    Now I'm just trying to get the variable data after it has been parsed from my JSON data into the label text so that it presents properly on the screen.

    Thanks
    John
     
  6. chown33, Jun 10, 2012
    Last edited: Jun 11, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    This doesn't change the fact that the property is bound to an ivar named _datetime. There is another ivar named tickdatetime, which doesn't appear to be bound to any property (per posted code). That ivar is also declared an IBoutlet. So which one do you think will receive the actual UILabel when loading the nib?


    The UILabel property named tickdatetime is separate and distinct from the ivar named tickdatetime.

    This code refers to the property:
    Code:
        self.tickdatetime.text = datetime;
    
    This code refers to the ivar:
    Code:
            tickdatetime.text = datetime;
    
    You can tell because there's no self. in front of the tickdatetime name.

    The reason that the tickdatetime property is not using the tickdatetime ivar is because of this line:
    Code:
    @synthesize tickdatetime = _datetime;
    
    This tells the compiler:
    "Synthesize the property named tickdatetime, but use the ivar _datetime to store and retrieve the value."
    If you'd left off the " = _datetime", then you'd be telling the compiler:
    "Synthesize the property named tickdatetime, using the ivar tickdatetime to store and retrieve the value."

    You're confused because you wrote code that's unnecessarily confusing and complex. Simplify the binding of properties to ivars (i.e. fix the @synthesize lines so the property name and ivar name are identical), then use properties consistently instead of using ivars in some places and properties in others.



    This refers to the property named tickdatetime. You still have posted code that refers to the ivar tickdatetime. You need to be consistent: property or ivar.


    I get the feeling you don't really understand the difference between a property and an ivar. I also sense you don't understand that this:
    Code:
    @synthesize tickdatetime = _datetime;
    
    binds the property named tickdatetime to an ivar whose name is not tickdatetime, yet you also have an ivar named tickdatetime. It binds the property named tickdatetime to an ivar named _datetime.

    Forget the JSON for a minute, and focus on coding a single identifiable destination for the data, either an ivar or a property, with a distinct unique name. If you can't get a distinct location to store the JSON text, then it doesn't really matter what the JSON text is.


    Several beginning iOS programming what? Books? Tutorials?

    What were the titles, authors? URLs of the tutorials?

    Did you complete the books or tutorials, or are you doing this program after completing only a part? What parts have you completed?

    We can't read your mind. We can't see the books on your desk. If you don't tell us what you've read and done, we can only guess.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    With the above code, I assume you have a @synthesize directive for it as well. Do you understand how that could be confusing alongside your other @synthesize directive?:
    Unless, of course, these are declared in two different classes, which you have not indicated. If they are, I believe you have a very different issue on your hands, one that is called 'data-persistence' but usually phrased by newcomers as 'sharing variables/values between two classes'.

    I think along with your code snippets, you need to provide some context as to where these bits of code reside. That is, what classes and files are they in?
     
  8. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #8
    Dejo and Chown

    I figured out what was happening and why the value for the label was showing up empty.

    I have the tickdatetime.text value in the didload before the variables were called and populated.

    Now I know where the problem is I will work it out so that the variables are called then the text done.

    Thanks for pointing me in the right direction.

    John
     

Share This Page