NSArray -initWithObjects: does not store objects - objective-c

I'm getting a really bizarre output from that contructor. It does not actually store any of the objects. I debugged the method and the objects being stored are initialized properly.
I use this array to set the vc's on a UITabBarController and the tab bar is empty. Here's the code
-(void)initBarItemsWithAllFeatures {
/*
Issues
*/
UIImage *issuesImage = [UIImage imageNamed:#"issues_on.png"];
UITabBarItem *issuesTabBarItem = [[UITabBarItem alloc]initWithTitle:NSLocalizedString(#"IssuesTabBarTitle",#"") image:issuesImage tag:0];
[issuesImage release];
issuesNavigationController.tabBarItem =issuesTabBarItem;
[issuesTabBarItem release];
/*
thumbs
*/
ThumbsViewController *thumbsViewController = [[ThumbsViewController alloc]initWithNibName:#"ThumbsViewController" bundle:nil];
UIImage *thumbsImage = [UIImage imageNamed:#"thumbs_on.png"];
UITabBarItem *thumbsTabBarItem = [[UITabBarItem alloc]initWithTitle:NSLocalizedString(#"ThumbsTabBarTitle",#"") image:thumbsImage tag:1];
[thumbsImage release];
thumbsViewController.tabBarItem = thumbsTabBarItem;
[thumbsTabBarItem release];
/*
contents
*/
ContentsViewController *contentsViewController = [[ContentsViewController alloc]initWithNibName:#"ContentsViewController" bundle:nil];
UIImage *contentsImage = [UIImage imageNamed:#"contents_on.png"];
UITabBarItem *contentsTabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString (#"ContentsTabBarTitle",#"") image:contentsImage tag:2];
[contentsImage release];
contentsViewController.tabBarItem = contentsTabBarItem;
[contentsTabBarItem release];
/*
search
*/
SearchViewController *searchViewController = [[SearchViewController alloc]initWithNibName:#"SearchViewController" bundle:nil];
UIImage *searchImage = [UIImage imageNamed:#"search_on.png"];
UITabBarItem *searchTabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString (#"SearchTabBarTitle",#"") image:searchImage tag:3];
[searchImage release];
searchViewController.tabBarItem = searchTabBarItem;
[searchTabBarItem release];
/*
favourites
*/
FavouritesViewController *favouritesViewController = [[FavouritesViewController alloc]initWithNibName:#"FavouritesViewController" bundle:nil];
UIImage *favouritesImage = [UIImage imageNamed:#"favourites_on.png"];
UITabBarItem *favouritesTabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString (#"FavouritesTabBarTitle",#"") image:contentsImage tag:4];
[favouritesImage release];
favouritesViewController.tabBarItem = favouritesTabBarItem;
[favouritesTabBarItem release];
/*
contact
*/
ContactViewController * contactViewController = [[ContactViewController alloc] initWithNibName:#"ContactViewController" bundle:nil];
UIImage *contactImage = [UIImage imageNamed:#"contact_on.png"];
UITabBarItem *contactTabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString (#"contactTabBarTitle",#"") image:contactImage tag:5];
[contactImage release];
contactViewController.tabBarItem = contactTabBarItem;
[contactTabBarItem release];
/*
add to an array
*/
allFeaturesAvailableTabBarItemArray = [[NSArray alloc] initWithObjects:
issuesNavigationController,
thumbsViewController,
contentsViewController,
searchViewController,
favouritesViewController,
contactViewController, nil];
/*
release objects
*/
[thumbsViewController release];
[contentsViewController release];
[searchViewController release];
[favouritesViewController release];
[contactViewController release];
}
Thanks in advance!

I think you may be over releasing the tab bar images (FWIW).

I ended up removing one by one the objects in the array to see which one was causing issues. the first object added was 0x0 (nil) so it didn't add anything to the array. The strange thing is that
on
issuesNavigationController.tabBarItem =issuesTabBarItem;
I'm calling properties on an nil object and fired no alarms. Is this "expected" or is it kind of a bug I should report?
Thank you very much to all of you for your quick answers.
I'm going to take care of the overrelease as well Thanks!

Related

UITabBarItem don't show any icon images?

I have this code:
MeuPrimeiroViewController *primeiro = [[MeuPrimeiroViewController alloc] init];
MeuSegundoViewController *segundo = [[MeuSegundoViewController alloc]init];
UITabBarController *tabbar = [[UITabBarController alloc] init];
tabbar.viewControllers = [NSArray arrayWithObjects:primeiro,segundo, nil];
primeiro.tabBarItem.title = #"Primeiro";
primeiro.tabBarItem.image = [UIImage imageNamed:#"1.jpg"];
segundo.tabBarItem.title = #"Segundo";
segundo.tabBarItem.image = [UIImage imageNamed:#"3.jpg"];
self.window.rootViewController = tabbar;
I don't know why, the images don't show?
You need to use the tabBarItem initWithTitle:image:selectedImage method instead. See the method definition here.

Can not adjust UIPopupController to display images

In my application (code listed below), I use a popover to display a series of colors that the user can choose. These colors are used for the color of the drawing they are completing above. I am trying to modify the popover to work the same way, except for this time I would want to display images (the images are saved in the application's documents folder as png files) instead of blocks of color. Listed below is the working code for the color selector popover. ColorGrid is a UIview which contains an NSArray Colors, as well as two NSUIntegers columnCount and rowCount. I have tried to replace the items in the colors array with UIImages of the png files, as well as UIImageViews but I have not been able to get a successful result (or a compilable one). Listed below is the working code. Could anyone show me how I can change the UIColor items to the images to show them in the grid?
- (IBAction)popoverStrokeColor:(id)sender {
StrokeColorController *scc = [[[StrokeColorController alloc] initWithNibName:#"SelectColorController" bundle:nil] autorelease];
scc.selectedColor = self.strokeColor;
[self doPopoverSelectColorController:scc sender:sender];
}
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
scc.colorGrid.colors = [NSArray arrayWithObjects:
//put the following below back in after testing
[UIColor blackColor],
[UIColor blueColor],
[UIColor redColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor orangeColor],
//[UIColor purpleColor],
// [UIColor brownColor],
// [UIColor whiteColor],
// [UIColor lightGrayColor],
//[UIColor cyanColor],
//[UIColor magentaColor],
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Thanks for your help. I am new to objective-c.
edit - heres the function with my attempt to insert the images instead of the colors
- (void)doPopoverSelectColorController:(SelectColorController*)scc sender:(id)sender {
[self setupNewPopoverControllerForViewController:scc];
scc.container = self.currentPopover;
self.currentPopover.popoverContentSize = scc.view.frame.size;
// these have to be set after the view is already loaded (which happened
// a couple of lines ago, thanks to scc.view...
scc.colorGrid.columnCount = 2;
scc.colorGrid.rowCount = 3;
//here we need to get the UIImage items to try to put in the array.
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
//here we need to add the file extension onto the file name before we add the name to the path
//[fileName appendString:#".hmat"];
NSString *strFile = [documentDirectory stringByAppendingPathComponent:#"test.png"];
NSString *strFile1 = [documentDirectory stringByAppendingPathComponent:#"test1.png"];
NSString *strFile2 = [documentDirectory stringByAppendingPathComponent:#"test2.png"];
NSString *strFile3 = [documentDirectory stringByAppendingPathComponent:#"test3.png"];
NSString *strFile4 = [documentDirectory stringByAppendingPathComponent:#"test4.png"];
NSString *strFile5 = [documentDirectory stringByAppendingPathComponent:#"test5.png"];
//now for the Images
UIImage *image = [ UIImage imageWithContentsOfFile: strFile];
UIImage *image1 = [ UIImage imageWithContentsOfFile: strFile1];
UIImage *image2 = [ UIImage imageWithContentsOfFile: strFile2];
UIImage *image3 = [ UIImage imageWithContentsOfFile: strFile3];
UIImage *image4 = [ UIImage imageWithContentsOfFile: strFile4];
UIImage *image5 = [ UIImage imageWithContentsOfFile: strFile5];
UIImageView *imageview = [[[UIImageView alloc] initWithImage:image] autorelease];
[self.view addSubview:imageview];
UIImageView *imageview1 = [[[UIImageView alloc] initWithImage:image1] autorelease];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[[UIImageView alloc] initWithImage:image2] autorelease];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[[UIImageView alloc] initWithImage:image3] autorelease];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[[UIImageView alloc] initWithImage:image4] autorelease];
[self.view addSubview:imageview4];
UIImageView *imageview5 = [[[UIImageView alloc] initWithImage:image5] autorelease];
[self.view addSubview:imageview5];
imageview.image = image;
imageview1.image = image1;
imageview2.image = image2;
imageview3.image = image3;
imageview4.image = image4;
imageview5.image = image5;
scc.colorGrid.colors = [NSArray arrayWithObjects:
// When attempting to add the images like this - get the error identified expected
// after the e in image, at the end bracket. Putting a * does nothing to change the error
[image],
// When adding one of the Imageviews, i get the same error as above
//below is how I attempted to add it
[imageView],
//
nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(colorSelectionDone:) name:ColorSelectionDone object:scc];
[self.currentPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; //displays the popover and anchors it to the button
}
Remove your square brackets around image and/or imageView :
scc.colorGrid.colors = [NSArray arrayWithObjects:
// Not : [image] but
image,
// or
imageView,
// Not : [imageView],
nil];

App crashes on start

Hi I was doing some testing earlier and my app was running just fine. I wanted to do some more testing so I decided to remove the app from my device and then reinstall it by running.
Well now for some reason I get to the stage where my splash screen shows up and then it crashes and I get the error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
This obviously means theres an array out of bounds correct? But why now and how can I found out where and what view controller this is happening on? How could it run before and now all of a sudden when I try reinstalling the app through running it again I get this error?
Thanks
EDIT
The error is with the array in the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
exploreViewController *view1 = [[exploreViewController alloc] initWithNibName:#"exploreViewController" bundle:nil];
view1.title= #"Explore";
Upcoming *view2 = [[Upcoming alloc] initWithNibName:#"Upcoming" bundle:nil];
view2.title = #"Upcoming";
calcViewController *view3 = [[calcViewController alloc] initWithNibName:#"calcViewController" bundle:nil];
view3.title = #"Calc";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:view2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:view3];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nil];
self.tabBarItem = [[[UITabBarItem alloc] init] autorelease];
NSArray *tabBarItems = self.tabBarController.tabBar.items;
UIImage *tab1 = [UIImage imageNamed:#"85-trophy.png"];
UIImage *tab2 = [UIImage imageNamed:#"12-eye.png"];
UIImage *tab3 = [UIImage imageNamed:#"237-key.png"];
NSArray *tabBarImages = [[[NSArray alloc] initWithObjects:tab1, tab2, tab3,nil]autorelease];
NSInteger tabBarItemCounter;
for (tabBarItemCounter = 0; tabBarItemCounter < [tabBarItems count]; tabBarItemCounter++)
{
tabBarItem = [tabBarItems objectAtIndex:tabBarItemCounter];
tabBarItem.image = [tabBarImages objectAtIndex:tabBarItemCounter];
}
Well, the reason for this crash is the following:
You are adding five items to your tabBar (nav1, nav2, nav3, nav4, nav6), but you only have three images for your tabs (tab1, tab2, tab3). So when the for loop reaches the fourth tab it crashes because tabBarImages only contains three objects.
Apart from that your code looks a bit messy - which could be the reason for not seeing the error on first sight.
// edit
You are complicating things - just try the following piece of code
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav1.title = #"Explore";
nav1.tabBarItem.image = [UIImage imageNamed:#"85-trophy.png"];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav2.title = #"Upcoming";
nav2.tabBarItem.image = [UIImage imageNamed:#"12-eye.png"];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:__your_viewController__];
nav3.title = #"Calc";
nav3.tabBarItem.image = [UIImage imageNamed:#"237-key.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:nav1, nav2, nav3, nil]];
[nav1 release];
[nav2 release];
[nav3 release];

Trying to load images in my slot machine iPhone app but getting an error (NSInvalidArgumentException)

I'm new to the hole Xcode/objective-c thing so it would be kind answering my questions in a way I can understand you :)
My problem is: I'm trying to create a slot machine with a picker and a button (to spin the wheels of course ;) ) so I'm implementing 5 images (apple,crown,cherry,lemon,seven,banana) but at the point when I start the app I get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage initWithImage:]: unrecognized selector sent to instance 0x4b649b0'
My code looks like this:
- (void)viewDidLoad {
UIImage *apple = [UIImage imageNamed:#"apple.png"];
UIImage *banana = [UIImage imageNamed:#"banana.png"];
UIImage *cherry = [UIImage imageNamed:#"cherry.png"];
UIImage *crown = [UIImage imageNamed:#"crown.png"];
UIImage *lemon = [UIImage imageNamed:#"lemon.png"];
UIImage *seven = [UIImage imageNamed:#"seven.png"];
for (int i = 1; i <= 5; i++) {
UIImageView *appleView = [[UIImage alloc] initWithImage:apple];
UIImageView *bananaView = [[UIImage alloc] initWithImage:banana];
UIImageView *cherryView = [[UIImage alloc] initWithImage:cherry];
UIImageView *crownView = [[UIImage alloc] initWithImage:crown];
UIImageView *lemonView = [[UIImage alloc] initWithImage:lemon];
UIImageView *sevenView = [[UIImage alloc] initWithImage:seven];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:appleView, bananaView, cherryView, crownView, lemonView, sevenView, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:#"column%d", i];
[self setValue: imageViewArray forKey:fieldName];
[fieldName release];
[imageViewArray release];
[appleView release];
[bananaView release];
[cherryView release];
[crownView release];
[lemonView release];
[sevenView release];
I've been looking around for typos quite some time but didn't find any. It would be really nice if someone could give me a hint. If the supported code isn't enough, feel free to ask for more.(Just copied the section where the error occurred) Sorry for my bad english but it's not my native language :)
/edit: I should've said, that the original error had been: SIGABRT, but it looks like this error seems to mean a lot without any context :)
You need to create your image views with
[[UIImageView alloc] initWithImage: ...]
NOT
[[UIImage alloc] initWithImage: ...]

Memory problem with EGOPhotoViewer

I use EGOPhotoView for my image local gallery. The number of images is 152 and their resolution is 1400x950px.
I have problems with the memory of my device! Memory does not get released. I display one image 45mb + next 55mb + next 72mb.....and over 130mb the app crashes.
In this code I have added images:
NSMutableArray *photos = [[NSMutableArray alloc] init];
for (Picture *picture in [self fetchedResultsController].fetchedObjects) {
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%#.jpg", picture.imgName]];
MyPhoto *photo = [[MyPhoto alloc] initWithImageURL:nil name:[NSString stringWithFormat:#"%#, %#, %#", picture.friendlyName, picture.type, picture.date] image:img painter:(Painter *)picture.painter];
[photos addObject:photo];
[photo release];
}
MyPhotoSource *source = [[MyPhotoSource alloc] initWithPhotos:[NSArray arrayWithArray:photos]];
EGOPhotoViewController *photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:photoController];
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:navController animated:YES];
[navController release];
[photoController release];
[source release];
[photos release];
I use EGOPhotoViewer as a modalView.
What could be the problem I'm having?
Even though you're explicitly releasing the instances of UIImage, the photos array that you're using still has a reference to them. This means that the images aren't being released from memory as it appears you're expecting.
I'm not familiar with EGOPhotoViewer, unfortunately. Is there a way you can lazy load the images? There are probably other options such as reducing the size of the images, but that's dependent upon what your requirements are.