Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2009, 05:36 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
blueFLoyd8 is on a distinguished road
Default Custom UIView Class : Using getter/Setter methods

Howdy!
I created a UIView so that I can easily add a customized object multiple times without using much code. The view object is a blue box with some dynamic text labels.
All of the code is working except I cannot edit the properties of the objects within the UIView. Take a look at the titleLbl object:

Main Class.m
This code successfully adds the blueBox, however I cannot change the text of the label to @"Hello World". The code initializes the label to say "hello, cosmos", but then I can't change this.
Code:
blueBox = [[blueBoxObject alloc] initWithFrame:CGRectMake(10,200,200,100)];
//[[blueBox titleLbl]setText:@"Hello World"];
blueBox.titleLbl.text = @"Hello, World!";
[self.view addSubview:blueBox];
blueBoxObject.h
Code:
@interface blueBoxObject : UIView {
	UIImageView *box;
	UILabel *titleLbl;
	UILabel *textLbl;
}
@property (nonatomic, retain) UILabel *titleLbl;
@property (nonatomic, retain) UILabel *textLbl;
blueBoxObject.m
Code:
- (void)drawRect:(CGRect)rect {
    // Drawing code
	box = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,BOX_W,BOX_H)]; 
	[box setImage:[UIImage imageNamed:@"btnBlue.png"]];
	box.opaque = YES;
	[self addSubview:box]; 
	
	titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
	titleLbl.backgroundColor = [UIColor clearColor];
	titleLbl.textColor = [[UIColor alloc] initWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
	titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
	titleLbl.textAlignment = UITextAlignmentLeft;
	[titleLbl setText:@"Hello, Cosmos"];
	[self addSubview:titleLbl];
}
Thanks for the help everyone!
blueFLoyd8 is offline   Reply With Quote
Old 07-12-2009, 02:33 AM   #2 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 400
Stitch is on a distinguished road
Default

Hi,

If you are using "@property (nonatomic, retain) UILabel *titleLbl;" then you need to make sure you use "self." otherwise you don't correctly go through the setter method.

eg:

Code:
UILabel *tempTitleLbl = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)]; // alloc the object here
self.titleLbl = tempTitleLbl; // pass the object through the setter so it's retained
[tempTitleLbl release]; // release the object that you allocated
titleLbl.backgroundColor = [UIColor clearColor];
titleLbl.textColor = [UIColor colorWithRed:0.7 green:0.16 blue:0.1 alpha:1.0]; // remove alloc from here as you are not releasing
titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
titleLbl.textAlignment = UITextAlignmentLeft;
[titleLbl setText:@"Hello, Cosmos"];
[self addSubview:titleLbl];
I'm not sure if it's required but I always update my strings from inside the actual View, not the main class:

Code:
- (void)myCustomUpdateLabelWithString:(NSString *)aString {
titleLbl.text = aString;
}
Don't forget to put this in your view header:
Code:
- (void)myCustomUpdateLabelWithString:(NSString *)aString;
Then just call this from your main class to update:

Code:
[blueBox myCustomUpdateLabelWithString:@"Updated String"];

Last edited by Stitch; 07-12-2009 at 02:36 AM.
Stitch is offline   Reply With Quote
Old 07-12-2009, 01:43 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
blueFLoyd8 is on a distinguished road
Default

That is not working for me. The NSLog statement in the updateTitle function prints the string value but the label does not get changed. Here is what I tried:
blueBoxObject.m
Code:
- (void)drawRect:(CGRect)rect {
    // Drawing code
	box = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,BOX_W,BOX_H)]; 
	[box setImage:[UIImage imageNamed:@"btnBlue.png"]];
	box.opaque = YES;
	[self addSubview:box]; 
	UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
	self.titleLbl = tempLabel;
	[tempLabel release];
	self.titleLbl.backgroundColor = [UIColor clearColor];
	self.titleLbl.textColor = [[UIColor alloc] initWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
	self.titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
	self.titleLbl.textAlignment = UITextAlignmentLeft;
	[self.titleLbl setText:@"temp"];
	[self addSubview:self.titleLbl];
}
- (void) updateTitleWithString:(NSString*)title {
	NSLog(@"updating. title = %@", title);
	[self.titleLbl setText:title];
}
Blueboxobject.h
Code:
@interface blueBoxObject : UIView {
	UIImageView *box;
	UILabel *titleLbl;
}
@property (nonatomic, retain) UILabel *titleLbl;

- (void) updateTitleWithString:(NSString*)title;
Main Class.m[
Code:
blueBox = [[blueBoxObject alloc] initWithFrame:CGRectMake(10,200,200,100)];
		//blueBox.titleLbl.text = @"Hello, World!";
		[blueBox updateTitleWithString:@"Danny"];
		[self.view addSubview:blueBox];
blueFLoyd8 is offline   Reply With Quote
Old 07-12-2009, 02:42 PM   #4 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 400
Stitch is on a distinguished road
Default

You only put "self." when you pass the object to it for retaining:

Code:
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
self.titleLbl = tempLabel;
[tempLabel release];
Everything else you can leave without it:

Code:
- (void)drawRect:(CGRect)rect {
    // Drawing code
	box = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,BOX_W,BOX_H)]; 
	[box setImage:[UIImage imageNamed:@"btnBlue.png"]];
	box.opaque = YES;
	[self addSubview:box]; 
	UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
	self.titleLbl = tempLabel;
	[tempLabel release];
	titleLbl.backgroundColor = [UIColor clearColor];
	titleLbl.textColor = [[UIColor alloc] initWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
	titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
	titleLbl.textAlignment = UITextAlignmentLeft;
	[titleLbl setText:@"temp"];
	[self addSubview:titleLbl];
}
- (void) updateTitleWithString:(NSString*)title {
	NSLog(@"updating. title = %@", title);
	[titleLbl setText:title];
}
Stitch is offline   Reply With Quote
Old 07-12-2009, 02:43 PM   #5 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 400
Stitch is on a distinguished road
Default

Again, I don't think you should do this:

Code:
[[UIColor alloc] initWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
Try without the "alloc". I think it's:
Code:
[UIColor colorWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
Stitch is offline   Reply With Quote
Old 07-12-2009, 03:13 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
blueFLoyd8 is on a distinguished road
Default

Still not working. The NSLog statement confirms that the parameter string is being passed.
Code:
- (void)drawRect:(CGRect)rect {
    // Drawing code
	box = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,BOX_W,BOX_H)]; 
	[box setImage:[UIImage imageNamed:@"btnBlue.png"]];
	box.opaque = YES;
	[self addSubview:box]; 
	UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
	self.titleLbl = tempLabel;
	[tempLabel release];
	titleLbl.backgroundColor = [UIColor clearColor];
	titleLbl.textColor = [UIColor colorWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
	titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
	titleLbl.textAlignment = UITextAlignmentLeft;
	[titleLbl setText:@"temp"];
	[self addSubview:titleLbl];
}
- (void) updateTitleWithString:(NSString*)title {
	NSLog(@"updating. title = %@", title);
	[titleLbl setText:title];
}
blueFLoyd8 is offline   Reply With Quote
Old 07-12-2009, 03:29 PM   #7 (permalink)
Registered Member
 
Stitch's Avatar
 
Join Date: Aug 2008
Posts: 400
Stitch is on a distinguished road
Default

Try taking your code out of drawRect and putting it in your own function:

Code:
- (void)createLabels {

<code here>

}
Then call this from your init in the view:

Code:
[self createLabels];
This way you know that the labels will be created when you init the view.

I think you're trying to set the label before it actually exists.
Stitch is offline   Reply With Quote
Old 07-12-2009, 08:35 PM   #8 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
blueFLoyd8 is on a distinguished road
Default

Quote:
Originally Posted by Stitch View Post
I think you're trying to set the label before it actually exists.
You were right. It turns out that drawRect() was getting called later. I am not sure why I even need this function. I moved the code to initWithFram() instead and it works now.
Thanks!

Code:
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
	self.backgroundColor = [UIColor clearColor];
	self.opaque = YES;
	box = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,BOX_W,BOX_H)]; 
	[box setImage:[UIImage imageNamed:@"btnBlue.png"]];
	box.opaque = YES;
	[self addSubview:box]; 
	[self drawLabels];
    return self;
}

- (void) drawLabels {
	NSLog(@"Draw labels");
	UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(15,15,BOX_W-30,40)];
	self.titleLbl = tempLabel;
	[tempLabel release];
	titleLbl.backgroundColor = [UIColor clearColor];
	titleLbl.textColor = [UIColor colorWithRed:0.7 green:0.16 blue:0.1 alpha:1.0];
	titleLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:32];
	titleLbl.textAlignment = UITextAlignmentLeft;
	[titleLbl setText:@"temp"];
	[self addSubview:titleLbl];
}
blueFLoyd8 is offline   Reply With Quote
Old 06-29-2010, 11:14 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 1
grosch is on a distinguished road
Default

You're leaking a reference here. When you call [UILabel alloc] you get a retain count of 1. When you assign to self.titleLbl it goes up to 2, which is why you then release it to go back to 1. Good stuff.

However, later you pass that into [self addSubview:titleLbl] which ups the reference count to 2 again. So you need to do one more release after the addView to get it back down to 1 again.
grosch is offline   Reply With Quote
Reply

Bookmarks

Tags
property, synthesize, uilabel, uiview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 321
10 members and 311 guests
chiataytuday, coolman, givensur, ipodphone, jbro, mottdog, mtl_tech_guy, Punkjumper, vilisei, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0