Okay, I’m new to programming in XCode and Objective-C, and have a question.
I can add a UIImageView normally using the drag and drop features in the Storyboard Builder thing. Then I can access it in code by creating a connection to the relavent ViewController, with a control drag and drop and then I can create a outlet. It is then declared in the *.h file with the @property line and synthesized in the *.m file.
BUT, what if I have a more complex view and it would take far too long drop and drag everything onto the screen and what if I wanted to change the position of the objects during execution?
I have tried using this in the ViewDidLoad Method:
NSMutableArray *mutableArray = [NSMutableArray array];
UIImage * pic = [UIImage imageNamed: @"Blue.png"];
for (int x = 0;x<10;x++){
for (int y =0;y<10;y++){
UIImageView *imageView = [[UIImageView alloc] initWithImage:pic];
imageView.frame =CGRectMake(x*23,y*41,32,41);
[mutableArray addObject:imageView];
}
}
This, I hoped would make a grid of UIImageViews and hold them in a NSMutableArray, but nothing appears on screen. How do you do a similar process to the drag and drop but in code? I realize I’m not synthesizing anything, and I only declare one instance UIImageView, but I thought If I put the declaration in the for loops then it would complain because it would be creating multiple variables with the same name. Is there anyway to create a load of UIImageViews in a few lines of code using a loop? I though of maybe just editing the sting used as the identifier for the pointer variable e.g. “image1”, “image2″….. But I didn’t know how to use a string type variable as the name of an identifier.
P.S. – I googled the CGRectMake procedure and the parameters are (x-coordinates,y-coordinates,width,height).
Any help would be greatly appreciated :).
You need to add them to the subview.
Instead of adding them to an array you could use tags to identify the individual
UIImageView, it all depends on what you prefer.Then to access the individual
UIImageView