I am trying to write my first iOS app that is not out of a book (though I guess I am stitching things together like crazy....)
At this point, I am attempting to initialize a mutable array, and then load it with images that are referenced by name (th开发者_高级运维ey are in with the project.)
This is what I have so far, and based upon what I see in the debugger, my array is empty.
- (void)viewDidLoad
{
txtLabel = [[UILabel alloc] init];
imageView = [[UIImageView alloc] init];
imageArray = [[NSMutableArray alloc] init];
seg = [[UISegmentedControl alloc] init];
imageArray = [[NSMutableArray arrayWithObjects:
[UIImage imageNamed:@"jupiter2.JPG"],
[UIImage imageNamed:@"waffles?.JPG"],
[UIImage imageNamed:@"enterprise.JPG"],
[UIImage imageNamed:@"wrunning.JPG"],
[UIImage imageNamed:@"ApolloCSM.JPG"],
nil] retain];
imageView.image = [UIImage imageNamed:@"happy-face.JPG"];
txtLabel.text = @"Hi there!";
[super viewDidLoad];
}
Thanks for any and all assistance.
Regards,
Steve O'Sullivan
You have
imageArray = [[NSMutableArray alloc] init];
and then
imageArray = [[NSMutableArray arrayWithObjects: ...
This leaks. Get rid of the first one.
But for the 2nd one, you need to use [[NSMutableArray alloc] initWithObjects:...
so that it sticks around.
You need to clarify "when" is that you're seeing the array empty. are you sure that viewDidLoad have been called when you're debugging?
and also... in the code sample you don't need this, by doing it you're creating a memory leak
imageArray = [[NSMutableArray alloc] init];
other than that there's nothing wrong with the way you construct the array
Try allocating the images on their own line and seeing if you have a pointer:
UIImage *jupImg = [UIImage imageNamed:@"jupiter2.JPG"];
If that's null then double check the file name is correct and that the "Copy Bundle Resources" is actually copying the image to where it can be found.
If you have a pointer try accessing some of its values like the width and height to see if they make sense.
--
EDIT
Wait a bit. I went off and thought about this and remembered a similar issue that I had a while ago.
I think the problem is that we're using mutable arrays and not static arrays. The method that you use to init the array is not fully defined for the mutable array.
I think you have to try something like:
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:[UIImage imageNamed:@"jupiter2.JPG"]];
... add other images here ...
After this I'm betting that you now actually have something in your array.
Try this..
-->Remove this. imageArray = [[NSMutableArray alloc] init];
-->Try this.
imageArray = [[NSMutableArray alloc]initWithObjects:
[UIImage imageNamed:@"jupiter2.JPG"],
[UIImage imageNamed:@"waffles?.JPG"],
[UIImage imageNamed:@"enterprise.JPG"],
[UIImage imageNamed:@"wrunning.JPG"],
[UIImage imageNamed:@"ApolloCSM.JPG"],
nil];
If there is no particular reason
[super viewDidLoad];
put this on top.
精彩评论