Set Text To label in Xcode

Discussion in 'Mac Programming' started by ahan.tm, Jun 26, 2011.

  1. macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    Hi,

    I was wondering how I can set a float variable to a label in a Mac App in Xcode 4. I currently have
    Code:
    	
    	["label name" setText:[NSString stringWithFormat:@"%.2f", "float variable"]];
    
    inside my IBAction. Is this correct?

    Thanks

    Ahan M.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    Does it compile? I can tell by looking at the code the answer is "No", so clearly, it's not correct.


    What book or tutorial are you learning from? Be specific.
    If the answer is "None", then tell us exactly what reference or other documentation you're using.

    Is this one of the exercises from a book or tutorial? Which one?
     
  3. thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    It does compile in xcode with the warning
    Code:
    Instance method '-setText:' not found (return type defaults to 'id')
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Treat the warning as an error. The compiler is telling you that you are sending a message that the object will not understand. Open the documentation and check the method.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Your code isn't even close to being correct:
    Code:
    ["label name" setText:[NSString stringWithFormat:@"%.2f", "float variable"]];
    
    The first thing after [ must be an object. "label name" isn't an object. It's a pointer to a C string. A C string is definitely not an object.

    There are other errors in that line of code, but they won't matter until you can write the code so it sends a message to an object.

    That's why I asked you to identify the book or tutorial you're learning from, or what reference doc you were using. Whatever it is, you really need to start with the basics and work up from there. It's clear from the posted code you don't yet have an adequate grasp of the fundamentals.
     
  6. thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #6
    Is the code the same for Mac and iPhone because

    Code:
    [altitude setText:[NSString stringWithFormat:@"%.2f", alt]];
    works fine in my iphone app.

    Also, are there any good tutorials out there?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    That's not the code you first posted. If altitude is an object that responds to setText:, and alt is a double, then that code will work.

    The problem is neither of those things is true for the code you first posted:
    1. "label name" is not an object.
    2. "float variable" is not a double variable.
    Both are C strings.

    So however you got that code to work in your iphone app, it seems that you don't really understand what it says or does.


    If you don't know how to find any tutorials, you should probably start at the beginning.

    At the top of this forum:
    http://forums.macrumors.com/forumdisplay.php?f=73

    there's a Guide link with this name:
    How do I get started programming on the Mac (or iPhone)?

    You should also see some other useful links.

    And you can search the MR site forums and look for keywords like beginner. Or just scroll through recent postings in the forum. The question comes up every couple of days.
     
  8. thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #8
    Hey,

    Thanks for taking the time to help me with my problem.

    After some working, I was able to get some parts of it working. The debugger now gives an error:

    Code:
    [42256:903] -[NSTextField setText:]: unrecognized selector sent to instance 0x10051ee40
    when using
    Code:
    [altitude setText:[NSString stringWithFormat:@"%.2f", alt]];
    Also, any great books on Objective-C?

    Thanks,
    Ahan
     
  9. macrumors 6502a

    Joined:
    Dec 29, 2009
    #9
    NSTextField doesn't have a method called setText:
    Take a look at the NSTextField Documentation

    Maybe you can find a method there that will suit your needs.
     
  10. macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Just a suggestion. If you are brand spanking new to programming start with C, not objective C. I am also learning to program and I stopped Objc - C and started C. Objective - C sits on top of C so much of that language you can use. More importantly you develop an understanding of programming. So many people follow tutorials and videos who know nothing, Monkey see Monkey do approach. Once you try to inject your own code into the tutorial to customize it you are just guessing and hoping it works, because you really have no clue what you are doing. I really started to learn to program a year ago and just 2 days ago I made my first GUI. I got the core information down first.

    If you want to have a good chance at succeeding in programming, buy this book "Learn C on the Mac". For me it was a great book to get the fundamentals from, and set me up to learn Objective - C.. I don't know the author or make money from it but well worth it.
     
  11. thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #11
    Hey Guys,

    Thanks for your responses. I finally figured it out using setStringValue instead of setText:

    Code:
     [altitude setStringValue:[NSString stringWithFormat:@"%.2f",alt]];
    Also, I have roots in C and C++ and it has been easy to adapt to Obj-C.

    Thanks Again For All Your Help!!:)
     

Share This Page