04-26-2009, 11:21 AM
#1 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
Objects in an Array
Hi,
I was told in another thread that if I wanted to keep track of an indefinite number of objects (in my case labels), I should put them in an array. I tried that, but I cannot retrieve the labels from the array.
I add the labels like this.
Code:
CGSize size = CGSizeMake(320-xCoordinate, 480-yCoordinate);
UILabel *message = [[UILabel alloc] init];
message.textAlignment = UITextAlignmentLeft;
message.lineBreakMode = UILineBreakModeWordWrap;
message.numberOfLines = 100;
message.font = [UIFont fontWithName:fontName size:fontSize];
message.textColor = colorForText;
message.backgroundColor = [UIColor clearColor];
message.text = messageField.text;
CGSize stringSize = [message.text sizeWithFont:message.font constrainedToSize:size lineBreakMode:message.lineBreakMode];
message.frame = CGRectMake(xCoordinate, yCoordinate, stringSize.width, stringSize.height);
[self addSubview:message];
[self sendSubviewToBack:message];
[textArray addObject:message];
[message release];
And if I want to remove one, say the last one added, I do this.
Code:
[[textArray lastObject] removeFromSuperview];
[textArray removeLastObject];
But it does not work. Please help! Thanks!
04-26-2009, 11:30 AM
#2 (permalink )
Registered Member
iPhone Dev SDK Supporter
Join Date: Oct 2008
Posts: 10
Are you using an NSArray or an NSMutableArray? NSArray doesn't support any of the methods you're calling.
04-26-2009, 11:33 AM
#3 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
Quote:
Originally Posted by
brandon0104
Are you using an NSArray or an NSMutableArray? NSArray doesn't support any of the methods you're calling.
No, I am using an NSMutable Array
04-26-2009, 11:38 AM
#4 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Define "does not work", please. That could mean a lot of different things.
04-26-2009, 11:40 AM
#5 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
Quote:
Originally Posted by
eddietr
Define "does not work", please. That could mean a lot of different things.
It seems that the Array is not retrieving the labels, and therefore does not remove them from the view.
04-26-2009, 11:43 AM
#6 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by
brian515
It seems that the Array is not retrieving the labels, and therefore does not remove them from the view.
At that point in your code, did you check that the array contains your labels?
You can check this in the debugger. Or just print the size of the array or something just to make the array has in it what you are expecting at that point.
04-26-2009, 11:45 AM
#7 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
Quote:
Originally Posted by
eddietr
At that point in your code, did you check that the array contains your labels?
No, how would I do that?
04-26-2009, 11:46 AM
#8 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by
brian515
No, how would I do that?
Sorry, I edited my post just as you were asking that.
04-26-2009, 11:50 AM
#9 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
No, they are not there. That is strange. I should technically be able to do this, right?
04-26-2009, 11:56 AM
#10 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 109
Quote:
Originally Posted by
brian515
No, they are not there. That is strange. I should technically be able to do this, right?
Whoops. Solved it! I forgot to allocate the array. Sorry!
04-26-2009, 11:56 AM
#11 (permalink )
Registered Member
Join Date: Apr 2009
Posts: 536
Quote:
Originally Posted by
brian515
No, they are not there. That is strange. I should technically be able to do this, right?
Yes, you should be able to do this. So somewhere between creating your array an adding these labels and then when you go looking for these labels, you've lost something.
EDIT: OK, posted at the same time again. Looks like you figured it out.
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
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl