04-21-2010, 11:49 AM
#1 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 12
Setting the Label
Hi!
I'm pretty new to cocoa and have a small problem that I hope somebody can help with.
Basically I can't get UILabel to display my "Hello" when I do the following in my ViewDidLoad.
Code:
[oldText initWithString:@"Hello"];
label.text = oldText;
oldText here is defined as follows
Code:
@interface DisplayViewController : UIViewController {
UILabel *label;
NSString *oldText;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) NSString *oldText;
As a check however, the following does work so everything seems to be connected properly.
Code:
label.text = @"This Works";
I'm guessing its got to do with how I'm defining oldText or some sort of incompatible type? But I'm quite clueless otherwise.
Thanks in advance!
04-21-2010, 12:37 PM
#2 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 259
setText
Code:
[myLabel setText:@"No! *this* should work"];
EDIT: so *this* also works:
Code:
[myLabel setText:[NSString stringWithFormat: @"%@", oldText]];
Last edited by finefin; 04-21-2010 at 12:42 PM .
04-21-2010, 12:40 PM
#3 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 12
Hi finefin!
Thanks for the reply!
That does work. Though, how do I get it to work with my instance oldText?
=)
04-21-2010, 12:42 PM
#4 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 259
see above
04-21-2010, 12:50 PM
#5 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 12
Thanks finefin!
It seems to be better, the label can now be seen. Though the text defined by oldText doesn't seem to appear.
Instead I get "(null)" appearing in the label.
04-21-2010, 01:03 PM
#6 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 259
hehe funny.
at this point you could use
Code:
oldText = @"Hello";
04-21-2010, 01:21 PM
#7 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 12
!!!!!!!!!!!!!!!!!
It works!!!!
And I have absolutely no idea why we can do that now!
Could you explain briefly or point me to a resource or something where i could read up on it?
This is totally messing me up... lol
04-21-2010, 01:47 PM
#8 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 259
the difference is quite simple.
UILabel is a text view, it inherits from UIView.
myLabel.text is an attribute of UILabel (besides font, textcolor, alignment...)
so if you want to set the text attribute of an UILabel, well, you use setText.
oldText is an NSString. it is just a string object. so you can set the string in that easy way.
You use initWithString when you create a new NSString object
Code:
NSString *myString = [[NSString alloc] initWithString:@"Hello"];
EDIT: for more info on the classes you use --> in xcode, mark the class name, right click and select "Find selected text in API reference", take your time and read.
Last edited by finefin; 04-21-2010 at 01:53 PM .
04-21-2010, 01:53 PM
#9 (permalink )
Registered Member
Join Date: Mar 2010
Posts: 12
Thanks for all your help!
=)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 317
20 members and 297 guests
baja_yu , cgokey , Domele , Duncan C , Fstuff , gbenna , givensur , guusleijsten , HowEver , iphonedevshani , jbro , JoeRCruso , mdpauley , n00b , newDev , seokwon lee , SLIC , stanny , Steven.C , WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,875
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031