tags:

views:

57

answers:

1

Hi- I'm having kind of a dumb problem.

I am using the below code to create/modify a UILabel via code. The reason I am creating it via code is because I need it to be rotated 90 degrees and Im not aware of a way to do that in IB.

What's happening - A user hits a button that makes the text they selected to appear in the UILabel. Then when they select the button again, with different text, the new text appears in place of the old text.

The first time I hit the button, it works perfectly, but the second time I hit the button, the new label appears over the old label and the old label never disappears. I have tried removing the first label, making it nil, just removing the text, but I cannot access any part of the label once it has been created.

ViewController.h

... UIView *viewForLabels; UILabel *tab1Label; } @property (nonatomic, retain) IBOutlet UIView *viewForLabels; @property (nonatomic, retain) IBOutlet UILabel *tab1Label ...

@end

ViewController.m

...

@synthesize tab1Label;

...

UILabel *tab1Label = [[UILabel alloc]init];

tab1Label.text = [theText];
tab1Label.backgroundColor = [UIColor clearColor];
tab1Label.textColor = [UIColor blackColor];
tab1Label.opaque = NO;
tab1Label.font = [UIFont systemFontOfSize:14];
tab1Label.numberOfLines = 2;
tab1Label.adjustsFontSizeToFitWidth=YES;
tab1Label.transform = CGAffineTransformMakeRotation (90*3.1459565) / 180);
tab1Label.frame = CGRectMake(2,87,45,119);
[viewForLabels: addSubview: tab1Label];

...

+1  A: 

First in your code example you alloc tabLabel1 and then run a bunch of property updates against a different named object tab1Label.

Sorry if I am misunderstanding the question but why are you creating a second label? Per this part of your description:

What's happening - A user hits a button that makes the text they selected to appear in the UILabel. Then when they select the button again, with different text, the new text appears in place of the old text.

Just update the .text property and any sizing needed why use a whole separate object?

Nick
Sorry that first thing was a typo on my part. The label wont appear if I dont have the alloc/init line.
Brodie
Okay I figured it out thanks to your comment. I had read somewhere that if you are going to rotate a label, to do it before the view is added so I was trying to completely build the label in code. I just removed the init/alloc line and added a label to IB connected to tab1Label and all works fine. Thanks for pointing me in the right direction.
Brodie
Happy it helped.You can manipulate a subview even after it is added to a view. Just hold on to your object or grab a pointer to it later and update your properties as needed. Adding a view as a subview just places it in the view hierarchy- you aren't losing the ability to manipulate it when you do so.
Nick