PDA

View Full Version : Set Text To label in Xcode




ahan.tm
Jun 26, 2011, 04:04 PM
Hi,

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


inside my IBAction. Is this correct?

Thanks

Ahan M.



chown33
Jun 26, 2011, 04:37 PM
Is this correct?

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?

ahan.tm
Jun 26, 2011, 04:45 PM
It does compile in xcode with the warning Instance method '-setText:' not found (return type defaults to 'id')

robbieduncan
Jun 26, 2011, 04:47 PM
It does compile in xcode with the warning Instance method '-setText:' not found (return type defaults to 'id')

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.

chown33
Jun 26, 2011, 05:14 PM
It does compile in xcode with the warning Instance method '-setText:' not found (return type defaults to 'id')
Your code isn't even close to being correct:
["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.

ahan.tm
Jun 26, 2011, 05:20 PM
Is the code the same for Mac and iPhone because

[altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

works fine in my iphone app.

Also, are there any good tutorials out there?

chown33
Jun 26, 2011, 05:51 PM
Is the code the same for Mac and iPhone because

[altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

works fine in my iphone app.

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.

ahan.tm
Jun 26, 2011, 07:00 PM
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:

[42256:903] -[NSTextField setText:]: unrecognized selector sent to instance 0x10051ee40 when using [altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

Also, any great books on Objective-C?

Thanks,
Ahan

Blakeasd
Jun 27, 2011, 09:02 AM
NSTextField doesn't have a method called setText:
Take a look at the NSTextField Documentation (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextField_Class/Reference/Reference.html)

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

larswik
Jun 27, 2011, 01:21 PM
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.

ahan.tm
Jun 27, 2011, 05:25 PM
Hey Guys,

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

[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!!:)