Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 26, 2011, 04:04 PM   #1
ahan.tm
macrumors regular
 
Join Date: Jun 2011
Location: Florida
Set Text To label in Xcode

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.
__________________
Parenting just got easier: Check out iBehave on the app store!
ahan.tm is offline   0 Reply With Quote
Old Jun 26, 2011, 04:37 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by ahan.tm View Post
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?
chown33 is offline   0 Reply With Quote
Old Jun 26, 2011, 04:45 PM   #3
ahan.tm
Thread Starter
macrumors regular
 
Join Date: Jun 2011
Location: Florida
It does compile in xcode with the warning
Code:
Instance method '-setText:' not found (return type defaults to 'id')
__________________
Parenting just got easier: Check out iBehave on the app store!
ahan.tm is offline   0 Reply With Quote
Old Jun 26, 2011, 04:47 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by ahan.tm View Post
It does compile in xcode with the warning
Code:
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.
robbieduncan is offline   0 Reply With Quote
Old Jun 26, 2011, 05:14 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by ahan.tm View Post
It does compile in xcode with the warning
Code:
Instance method '-setText:' not found (return type defaults to 'id')
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.
chown33 is offline   0 Reply With Quote
Old Jun 26, 2011, 05:20 PM   #6
ahan.tm
Thread Starter
macrumors regular
 
Join Date: Jun 2011
Location: Florida
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?
__________________
Parenting just got easier: Check out iBehave on the app store!
ahan.tm is offline   0 Reply With Quote
Old Jun 26, 2011, 05:51 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by ahan.tm View Post
Is the code the same for Mac and iPhone because

Code:
[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.
chown33 is offline   0 Reply With Quote
Old Jun 26, 2011, 07:00 PM   #8
ahan.tm
Thread Starter
macrumors regular
 
Join Date: Jun 2011
Location: Florida
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
__________________
Parenting just got easier: Check out iBehave on the app store!
ahan.tm is offline   0 Reply With Quote
Old Jun 27, 2011, 09:02 AM   #9
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
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.
Blakeasd is offline   0 Reply With Quote
Old Jun 27, 2011, 01:21 PM   #10
larswik
macrumors 65816
 
Join Date: Sep 2006
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.
larswik is offline   0 Reply With Quote
Old Jun 27, 2011, 05:25 PM   #11
ahan.tm
Thread Starter
macrumors regular
 
Join Date: Jun 2011
Location: Florida
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!!
__________________
Parenting just got easier: Check out iBehave on the app store!
ahan.tm is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to set labels at dictionary in Xcode? carolpengmusic Mac Programming 3 Mar 12, 2014 06:36 AM
Set label title from mouseEntered: on a NSButton Val K Mac Programming 1 Jun 19, 2013 03:39 AM
xCode label text color change kushalashok Web Design and Development 1 Feb 15, 2013 05:22 AM
What's the diff between text field and label ? iNeedAnsurs iPhone/iPad Programming 6 Nov 19, 2012 03:40 AM
ios label.text null wordgeist iPhone/iPad Programming 3 Sep 15, 2012 08:01 AM

Forum Jump

All times are GMT -5. The time now is 03:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC