This is my first question on Stack Overflow, so I hope that I’m clear enough.
I am designing a game for the iPhone, and am having trouble displaying some objects. I have an NSMutableArray of objects (a custom object class that subclasses UIImageView), and I want to be able to display them all on the screen. However, the array can change size, so I can’t directly link each element in interface builder. Does anyone have any idea on how I can start solving this issue?
Thanks!
The Code:
//This code is in the viewController.m file
//The main array of objects is nodeList. Each node subclasses UIImageView.
//nodeList was also synthesized at the beginning of the file with @synthesize.
//initialize array
nodeList= [[NSMutableArray alloc] initWithCapacity:(vertLinks*horizLinks)];
//make center node
Node *n = [Node alloc];
//Initialize the node (just puts an x and y position in, and gives it a mass)
[n nodeInit:(startX):(startY):(nodeMass)];
[nodeList addObject:(n)];
//creates a 'web' of nodes, and adds each one to the array
for (int i = 1; i < vertLinks; i++)
{
for (int j = 0; j < horizLinks; j++)
{
//draw the line to each point
int nodeX = (startX + cos(((360 / horizLinks) * j) * (M_PI / 180)) * rad * i);
int nodeY = (startY + sin(((360 / horizLinks) * j) * (M_PI / 180)) * rad * i);
//add it to the screen
Node *nd = [Node alloc];
[nd nodeInit:(nodeX):(nodeY):(nodeMass)];
if (i == vertLinks - 1)
{
nd.solid = true;
}
[nodeList addObject:(nd)];
}
}
Kinda hard to answer precisely but essentially you’ll need to loop through the objects in the array, and add each one separately. If these objects are views like UIView, UIImageView or UIButton, then you can just do this:
If they are strings or numbers or something then you’ll need to create a label or a text field or something to display the objects.
It really matters what object type you’re talking about. You need to tell us what kind of images these are if you need any real help. It’s also just the proper way to post.
Please post more details and I’ll be happy to continue my answer.