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 04-26-2009, 11:21 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default 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!
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:30 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
Join Date: Oct 2008
Posts: 10
brandon0104 is on a distinguished road
Default

Are you using an NSArray or an NSMutableArray? NSArray doesn't support any of the methods you're calling.
brandon0104 is offline   Reply With Quote
Old 04-26-2009, 11:33 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default

Quote:
Originally Posted by brandon0104 View Post
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
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:38 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

Define "does not work", please. That could mean a lot of different things.
eddietr is offline   Reply With Quote
Old 04-26-2009, 11:40 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default

Quote:
Originally Posted by eddietr View Post
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.
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:43 AM   #6 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

Quote:
Originally Posted by brian515 View Post
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.
eddietr is offline   Reply With Quote
Old 04-26-2009, 11:45 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default

Quote:
Originally Posted by eddietr View Post
At that point in your code, did you check that the array contains your labels?
No, how would I do that?
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:46 AM   #8 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

Quote:
Originally Posted by brian515 View Post
No, how would I do that?
Sorry, I edited my post just as you were asking that.
eddietr is offline   Reply With Quote
Old 04-26-2009, 11:50 AM   #9 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default

No, they are not there. That is strange. I should technically be able to do this, right?
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:56 AM   #10 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
brian515 is on a distinguished road
Default

Quote:
Originally Posted by brian515 View Post
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!
brian515 is offline   Reply With Quote
Old 04-26-2009, 11:56 AM   #11 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 536
eddietr is on a distinguished road
Default

Quote:
Originally Posted by brian515 View Post
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.
eddietr is offline   Reply With Quote
Reply

Bookmarks

Tags
array, indefinite, label, object, remove

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: 315
7 members and 308 guests
blueorb, givensur, guusleijsten, jbro, mer10, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
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:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0