iPhone crash.. no console error - iphone

Update
I turned Zombies on... and got this error:
*** -[CALayer retain]: message sent to deallocated instance 0x709d1a0
I'm experiencing a crash that does not make any sense to me. In loadMediaList I'm programmatically creating multiple buttons and assigning an action(openMedia:) to them. That action has an "openURL" action. Everything works great, until the action is called; the action is performed, opened up in a new window, but then crashes once the app is exited. I replaced the "openURL" code with different code, and it still crashes... thoughts?
There is no error in the console, I get EXC_BAD_ACCESS. The console says:
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
kill
quit
Program ended with exit code: 0
I have been working on this issue for a few days now and I cannot figure it out. my dealloc is great, threads seem to be working well, etc. I don't know if this is legal on Stack Overflow, but I will gladly pay someone to help me solve this. I have a very tight deadline.
Hope you can help!
-(IBAction)showMedia:(id)sender {
NSLog(#"Media Button was pressed");
//Begin Alert
[SVProgressHUD showInView:self.view status:#"Loading..."];
mediaScroll.delegate = self;
mediaScroll.frame = CGRectMake(15, 60, 240, 185);
mediaScroll.clipsToBounds = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
statsBTN.frame = CGRectMake(272, 76, 48, 46);
donateBTN.frame = CGRectMake(272, 124, 48, 46);
shareBTN.frame = CGRectMake(272, 172, 48, 46);
mediaBTN.frame = CGRectMake(280, 220, 48, 46);
incViewPopUP.alpha = 0;
donViewPopUP.alpha = 0;
shareViewPopUP.alpha = 0;
mediaViewPopUP.alpha = 1;
[UIView commitAnimations];
[self loadMediaList];
[mediaBTN addTarget:self action:#selector(closePopUp:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)loadMediaList {
[self.productPointers removeAllObjects];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"date_reported" ascending:NO] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [finalMediaList sortedArrayUsingDescriptors:sortDescriptors];
NSLog(#"Sorted Media Array: %#", sortedArray);
if (mediaLoaded == NO) {
NSDictionary *mediaPost;
for (mediaPost in sortedArray) {
NSDictionary *inside = (NSDictionary *)[mediaPost valueForKey:#"media"];
NSLog(#"Inside Array: %#", inside);
UIButton *mediaView = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
mediaView.frame = CGRectMake(0, (mx * 100), 225, 100);
mediaView.backgroundColor = [UIColor clearColor];
[mediaView addTarget:self action:#selector(openMedia:) forControlEvents:UIControlEventTouchUpInside];
mediaView.titleLabel.hidden = YES;
mediaView.titleLabel.alpha = 0;
mx++; NSLog(#"MX: %i", mx);
UILabel *mediaDesc = [[UILabel alloc] init];
mediaDesc.frame = CGRectMake(50, 20, 154, 40);
mediaDesc.backgroundColor = [UIColor clearColor];
mediaDesc.font = [UIFont fontWithName:#"Geogrotesque" size:12];
mediaDesc.textColor = UIColorFromRGB(0xc7c7c7);
mediaDesc.numberOfLines = 0;
mediaDesc.lineBreakMode = UILineBreakModeWordWrap;
mediaDesc.text = [inside valueForKey:#"description"];
UILabel *mediaType = [[UILabel alloc] init];
mediaType.frame = CGRectMake(50, 40, 154, 50);
mediaType.backgroundColor = [UIColor clearColor];
mediaType.font = [UIFont fontWithName:#"Geogrotesque" size:12];
mediaType.textColor = UIColorFromRGB(0xffffff);
mediaType.numberOfLines = 0;
mediaType.lineBreakMode = UILineBreakModeWordWrap;
mediaType.text = [[inside valueForKey:#"type"] uppercaseString];
UIImageView *mediaBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0, 99.0, 220.0, 1.0)];
[mediaBorder setImage:[UIImage imageNamed:#"bottom_border.png"]];
UIImageView *mediaArrow = [[UIImageView alloc] initWithFrame:CGRectMake(214.0, 45.0, 6.0, 9.0)];
[mediaArrow setImage:[UIImage imageNamed:#"media_right_arrow.png"]];
if ([mediaType.text isEqualToString:#"VIDEO"]) {
UIImageView *mediaThumb = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 35.0, 30.0, 30.0)] autorelease];
[mediaThumb setImage:[UIImage imageNamed:#"media_play_icon.png"]];
[mediaView addSubview:mediaThumb];
[mediaThumb release];
[mediaView setTag:1];
[mediaView setTitle:[inside valueForKey:#"filename"] forState:UIControlStateNormal];
}
if ([mediaType.text isEqualToString:#"IMAGE"]) {
UIImageView *mediaThumb = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 35.0, 30.0, 22.0)] autorelease];
[mediaThumb setImage:[UIImage imageNamed:#"media_photo_icon.png"]];
[mediaView addSubview:mediaThumb];
[mediaThumb release];
[mediaView setTag:2];
[mediaView setTitle:[inside valueForKey:#"url"] forState:UIControlStateNormal];
}
[mediaView addSubview:mediaArrow];
[mediaView addSubview:mediaBorder];
[mediaView addSubview:mediaDesc];
[mediaView addSubview:mediaType];
[mediaScroll addSubview:mediaView];
[self.productPointers addObject:mediaView];
[mediaArrow release];
[mediaBorder release];
[mediaType release];
[mediaDesc release];
[mediaView release];
}
}
mediaLoaded = YES;
[mediaScroll setContentSize:CGSizeMake(225.0f, (mx * 100))];
//End Alert
[SVProgressHUD dismiss];
}
-(IBAction)openMedia:(id)sender {
NSLog(#"Media opened!");
NSString *tag = [NSString stringWithFormat:#"%d", [sender tag]];
NSLog(#"Tag: %#", tag);
videoID = [sender currentTitle];
NSString *videoURL = [[[NSString alloc] initWithFormat:#"http://www.vimeo.com/%#", videoID] autorelease];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
NSLog(#"Video URL: %#", videoURL);
}

Turn on malloc stack logging, guard malloc, and zombie enabled in the debugger, then run:
(gdb) info malloc-history 0x1b18b0
Where 0x1b18b0 is the address of the thing that has the bad access error. It should give you more info about where in your code the problem is.
See this article for better instructions
Also, some code thoughts that may help
change this:
videoID = [sender currentTitle];
NSString *videoURL = [[[NSString alloc] initWithFormat:#"http://www.vimeo.com/%#", videoID] autorelease];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
To:
videoID = [sender currentTitle];
if (videoID) {
// no need to alloc then set autorelease, just used a named initializer since they autorelease by default:
NSString *videoURL = [NSString stringWithFormat:#"http://www.vimeo.com/%#", videoID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:videoURL]];
} else {
// deal
}

Related

ImageGallery image in black for ios 7

In my efforts to upgrade my application to support IOS7 I found out that ImageGallery don't load the images. in others iOS is ok.
In imageGalleryView:
- (void)initWithPhotos:(NSMutableArray *)photoURLStrings andCaptions:(NSArray *)myCaptions moveToPage:(int)page {
captions = [myCaptions copy];
photoUrls = [photoURLStrings copy];
NSLog(#"array---> %#", photoUrls);
photoLoaded = [[NSMutableArray alloc] init];
for (int i=0; i<[photoURLStrings count]; i++) {
[photoLoaded addObject:[NSNumber numberWithBool:FALSE]];
}
[pageControl setNumberOfPages:[photoUrls count]];
//scrollView= [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor blackColor];
scrollView.pagingEnabled = TRUE;
scrollView.autoresizesSubviews = TRUE;
scrollView.contentSize = CGSizeMake(320 * [photoUrls count], scrollView.frame.size.height);
scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width*page, 0);
if (([captions objectAtIndex:page] == nil) || ([[captions objectAtIndex:page] isEqualToString:#""])) {
[textView setHidden:TRUE];
} else {
[textView setHidden:FALSE];
[textView setText:[captions objectAtIndex:page]];
}
[self showImages:page];}
- (void)showImages:(int)page {
AsyncImageViewController *asyncImageView;
if ((page>=0)&&(page<[photoUrls count])) {
if (![[photoLoaded objectAtIndex:page] boolValue]) {
[photoLoaded replaceObjectAtIndex:page withObject:[NSNumber numberWithBool:TRUE]];
asyncImageView = [[AsyncImageViewController alloc] init];
//NSLog(#"%#",[NSString stringWithFormat:#"%#",[photoUrls objectAtIndex:page]]);
[asyncImageView loadImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[photoUrls objectAtIndex:page]]] pos:1];
//NSLog(#"page:%i",page);
asyncImageView.frame = CGRectMake(320*page,0,320,scrollView.frame.size.height);
//[scrollView setBackgroundColor:[UIColor colorWithPatternImage:asyncImageView.image]];
[scrollView addSubview:asyncImageView];
[asyncImageView release];
}
}
page = page - 1;
if ((page>=0)&&(page<[photoUrls count])) {
if (![[photoLoaded objectAtIndex:page] boolValue]) {
[photoLoaded replaceObjectAtIndex:page withObject:[NSNumber numberWithBool:TRUE]];
asyncImageView = [[AsyncImageViewController alloc] init];
[asyncImageView loadImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[photoUrls objectAtIndex:page]]] pos:1];
asyncImageView.frame = CGRectMake(320*page,0,320,scrollView.frame.size.height);
[scrollView addSubview:asyncImageView];
[asyncImageView release];
}
}
page = page + 2;
if ((page>=0)&&(page<[photoUrls count])) {
if (![[photoLoaded objectAtIndex:page] boolValue]) {
[photoLoaded replaceObjectAtIndex:page withObject:[NSNumber numberWithBool:TRUE]];
asyncImageView = [[AsyncImageViewController alloc] init];
[asyncImageView loadImageFromURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[photoUrls objectAtIndex:page]]] pos:1];
asyncImageView.frame = CGRectMake(320*page,0,320,scrollView.frame.size.height);
[scrollView addSubview:asyncImageView];
//[scrollView setBackgroundColor:[UIColor colorWithPatternImage:asyncImageView.image]];
[asyncImageView release];}}}
In the asyncimageview:
- (void)loadImageFromURL:(NSURL*)url pos:(int) posicio {
if (connection!=nil) { [connection release]; }
if (data!=nil) { [data release]; }
NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
if (posicio == 0) {
loading.center = CGPointMake(75/2,75/2);
}else {
loading.center = CGPointMake(160,210);
}
[loading startAnimating];
[self addSubview:loading];
[loading release];}//the URL connection calls this repeatedly as data arrives- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
[data appendData:incrementalData];}//the URL connection calls this once all the data has downloaded- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
//so self data now has the complete image
[connection release];
connection=nil;
if ([[self subviews] count]>0) {
//then this must be another image, the old one is still in subviews
[[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
}//make an image view for the image
imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
//[self setBackgroundColor:[UIColor colorWithPatternImage:imageView]];
[self addSubview:imageView];
imageView.frame = self.bounds;
[imageView setNeedsLayout];
[self setNeedsLayout];
[loading stopAnimating];
[data release]; //don't need this any more, its in the UIImageView now
data=nil;}
//just in case you want to get the image directly, here it is in subviews- (UIImage*) image {
UIImageView* iv = [[self subviews] objectAtIndex:0];
return [iv image];}
I checked all and saw that it is UIView instead of UIImageView. probably Apple changed something. But xCode don't throw any errors.
Any idea how to fix it?

How to show image loaded from a url on a button?

Here is an example :
Show multi image on multi button and show on scrollview
(void)viewDidLoad
{
[super viewDidLoad];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
InitData *inData=[[InitData alloc]init];
scrollview.delegate=self;
scrollview.scrollEnabled = YES;
int scrollWidth = 120;
scrollview.contentSize = CGSizeMake(scrollWidth,100);
int xOffset = 0;
//imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
//Test
NSURL *url=[NSURL URLWithString:#"....."];
NSData *jsonData=[NSData dataWithContentsOfURL:url];
if (jsonData !=nil)
{
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error ==nil)
{
NSLog(#"%#",result);
// NSMutableArray *ArImg = nil;
NSArray* rus=[result objectForKey:#"RespOBJ"];
for (int index = 0; index < rus.count ; index++)
{
NSMutableDictionary* final=[rus objectAtIndex:index];
[inData setId:(int)[final objectForKey:#"Id"]];
[inData setTum:[final objectForKey:#"TumbImage"]];
[inData setImage:[final objectForKey:#"Image"]];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:[inData getImage]] forState:(UIControlStateNormal)];
//[btn setImage:[UIImage imageWithData:jsonData] forState:(UIControlStateNormal)];
[btn setTag:index];
btn.bounds = CGRectMake(10, 10, 50, 50);
btn.frame = CGRectMake(5+xOffset, 0, 160, 70);
scrollview.contentSize = CGSizeMake(scrollWidth+xOffset,70);
NSLog(#"%#",[inData getId]);
NSLog(#"%#",[inData getImage]);
NSLog(#"%#",[inData getTum]);
[btn addTarget:self
action: #selector(btnclick:)
forControlEvents: UIControlEventTouchDown];
[scrollview addSubview:btn];
xOffset += 170;
}
The problem with your code is that you are not loading anything. I assume you want to download the image from the Internet and then display it on the scrollview. To do so, you have to use a NSURLRequest and a NSURLConnection.
Have a look at Apple's documentation https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html and https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
You might also like this question on how to load data asynchronously Object-c/iOS :How to use ASynchronous to get a data from URL?

Refresh view or remove subviews ios

I'm trying to make a scene where the user can swipe over the screen to browse between posts. The post can be both an image with text or just a note, and the view is altered depending on which one occurs.
The getting process works just perfect. It gets the right post whether i swipe right or left. The problem is that the old view wont disappear and the views are overlapping. This is especially bothering when you go from a note to a photo or vice versa since the sizes ar different...
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#" GETRESULT %# POSTNUMBER %d", getResult, postNumber);
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
UISwipeGestureRecognizer *recognizerRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
[[self view] addGestureRecognizer:recognizerRight];
UISwipeGestureRecognizer *recognizerLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
[recognizerLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:recognizerLeft];
// Do any additional setup after loading the view.
}
- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE RIGHT");
if(postNumber > 0){
postNumber--;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if (getImageString) {
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer{
NSLog(#"SWIPE LEFT");
if(postNumber < [getResult count] - 1){
postNumber++;
Post *postInfo = [[Post alloc] init];
postInfo = [getResult objectAtIndex:postNumber];
UITextView *postText = [[UITextView alloc] init];
imgView = [[UIImageView alloc] init];
NSString *getImageString = postInfo.attachments;
if(getImageString){
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 320, 280, 80)];
NSLog(#"IMG1");
}else {
[postText removeFromSuperview];
postText = [[UITextView alloc] init];
postText.text = postInfo.noteText;
[postText setFrame:CGRectMake(20, 10, 280, 240)];
NSLog(#"TEXT1");
}
[self.view addSubview:postText];
SHOWHUD(self.view);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if(getImageString){
NSURL *url = [NSURL URLWithString:getImageString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[imgView setFrame:CGRectMake(20, 10, 280, 300)];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
[imgView setImage:img];
}else {
[imgView removeFromSuperview];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:imgView];
HIDEHUD(self.view);
});
});
}
}
At first i only increased postNumber and called viewDidLoad in the swipe actions. That gave me the exact same result though... This is only one of countless trial and error attempts, and I'm sorry this is among the messier ones...
Would greatly appreciate it if anyone got a solution.
Thanks in advance,
Tom
Did you try this?
for (UIView * view in self.view.subviews) {
if ([view isEqual:postText]||[view isEqual:imgView]) {
[view removeFromSuperview];
}
}
I couldn't remove the variables from the superview because I did it in a new instance of viewDidLoad. I tried to do so before calling viewDidLoad this time and it worked!
Sorry for wasting your time...
Tom

UIButton view unrecognized selector sent to instance

Basically i have a UIToolbar on the main window and when i hit the infobutton to open a modal view application is crashing with output message that UIButton view unrecognized selector sent to instance. Can someone please help me to solve this output message.
UIButton *infoItem = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoItem.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
infoItem.backgroundColor = [UIColor clearColor];
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
- (void) playaudio: (id) sender {
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
// Making sure the audio is at the start of the stream.
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
- (void)stop: (id) sender
{
[audioPlayer stop];
}
- (void)displayModalViewaction
{
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
}
Appreciate help to solve this.
Thanks in advance.
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
should be
[infoItem addTarget:self action:#selector(displayModalViewaction) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
because displayModalViewaction do not receive any parameters.
use #selector(displayModalViewaction) to add event;
or change method define with: -(void)displayModalViewaction:(id)sender;

NSScrollView gives error when scrolling "-[CALayer isEqual:]: message sent to deallocated instance"

I'm trying to fix my code but I have no idea why I'm getting these errors:
modifying layer that is being finalized - 0x11818240
2010-09-10 02:35:53.022 iPhone Monkey Curling[1606:207] modifying layer that is being finalized - 0x11818240
2010-09-10 02:35:53.023 iPhone Monkey Curling[1606:207] modifying layer that is being finalized - 0x11818240
2010-09-10 02:35:54.671 iPhone Monkey Curling[1606:207] *** -[CALayer isEqual:]: message sent to deallocated instance 0x11818240
This happens when my all_score_table scroll view has had an attempt to scroll it (Crashes immediately on scroll attempt). Here is my code which sets the scroll views:
NSMutableArray * scores_data = [[first_data componentsSeparatedByString: #"\n"] mutableCopy];
if ([scores_data count] != 3) {
UIAlertView *connection_alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Malformed server response." delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil];
[connection_alert show];
[connection_alert release];
return;
}
if([[scores_data objectAtIndex:0] isEqual:#""]){
[scores_data replaceObjectAtIndex:0 withObject:#"No scores for today,"];
}
if([[scores_data objectAtIndex:1] isEqual:#""]){
[scores_data replaceObjectAtIndex:1 withObject:#"No scores this week,"];
}
if([[scores_data objectAtIndex:2] isEqual:#""]){
[scores_data replaceObjectAtIndex:2 withObject:#"No scores,"];
}
[scores_data replaceObjectAtIndex:0 withObject: [[scores_data objectAtIndex:0] componentsSeparatedByString: #";"]];
[scores_data replaceObjectAtIndex:1 withObject: [[scores_data objectAtIndex:1] componentsSeparatedByString: #";"]];
[scores_data replaceObjectAtIndex:2 withObject: [[scores_data objectAtIndex:2] componentsSeparatedByString: #";"]];
NSLog(#"scores_data");
if (todays_scores_table){
//One of the tables exist so all of them must. Release them for renewal.
[todays_scores_table release];
[week_scores_table release];
[all_scores_table release];
[online_scores_pages release]; //release pages also
}
NSLog(#"1");
todays_scores_table = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 110, 295, 300)];
week_scores_table = [[UIScrollView alloc] initWithFrame:CGRectMake(330, 110, 295, 300)];
all_scores_table = [[UIScrollView alloc] initWithFrame:CGRectMake(650, 110, 295, 300)];
todays_scores_table.contentSize = CGSizeMake(205, 60 + 40*[[scores_data objectAtIndex:0] count]);
week_scores_table.contentSize = CGSizeMake(205, 60 + 40*[[scores_data objectAtIndex:1] count]);
all_scores_table.contentSize = CGSizeMake(205, 60 + 40*[[scores_data objectAtIndex:2] count]);
NSLog(#"2");
NSArray * score_data;
NSString * path = [[NSBundle mainBundle] pathForResource: #"single_score_image" ofType:#"png"];
NSData * data = [[NSData alloc] initWithContentsOfFile:path];
UIImage * background_image = [[UIImage alloc] initWithData:data];
UIImageView * background_view;
UIImage * header_image;
if(submission){
path = [[NSBundle mainBundle] pathForResource: #"scores_header" ofType:#"png"];
[data release];
NSLog(#"3");
data = [[NSData alloc] initWithContentsOfFile:path];
header_image = [[UIImage alloc] initWithData:data];
}
UIImageView * header_view;
int offset;
UILabel * label;
NSLog(#"4");
UIScrollView * tables[3] = {todays_scores_table,week_scores_table,all_scores_table};
online_scores_pages = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
online_scores_pages.contentSize = CGSizeMake(960, 420);
online_scores_pages.pagingEnabled = YES; //Act like iphone home page
UIImage * title_image;
UIImageView * title_view;
NSLog(#"5");
//border for scores table
[data release];
path = [[NSBundle mainBundle] pathForResource: #"score_table_border" ofType:#"png"];
data = [[NSData alloc] initWithContentsOfFile:path];
UIImage * border_image = [[[UIImage alloc] initWithData:data] autorelease];
UIImageView * score_table_border;
for (int a = 0; a < 3; a++) {
if(submission){ //Only do header when submitting score
header_view = [[UIImageView alloc] initWithImage: header_image];
header_view.frame = CGRectMake(10, 10, 275, 30);
NSLog(#"5A");
[tables[a] addSubview: header_view];
[header_view release];
label = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 245, 30)];
label.backgroundColor = [UIColor clearColor];
NSLog(#"5B");
NSString * end;
NSLog(#"5C");
if ([[scores_data objectAtIndex:a+3] characterAtIndex:0] == '1'){
end = #"st";
}else if ([[scores_data objectAtIndex:a+3] characterAtIndex:0] == '2'){
end = #"nd";
}else if ([[scores_data objectAtIndex:a+3] characterAtIndex:0] == '3'){
end = #"rd";
}else{
end = #"th";
}
NSArray * pos_data = [[scores_data objectAtIndex:a+3] componentsSeparatedByString: #"/"];
label.text = [NSString stringWithFormat: #"You came %#%# from %# scores.",[pos_data objectAtIndex:0],end,[pos_data objectAtIndex:1]];
[tables[a] addSubview:label];
NSLog(#"5E");
[label release];
NSLog(#"6");
}
for (int x = 0; x < [[scores_data objectAtIndex:a] count]; x++){
score_data = [[[scores_data objectAtIndex:a] objectAtIndex:x] componentsSeparatedByString: #","];
//Image for each score
offset = 50 + 40*x;
background_view = [[UIImageView alloc] initWithImage: background_image];
background_view.frame = CGRectMake(10, offset, 275, 30); //Reposition
[tables[a] addSubview: background_view];
[background_view release];
//Add score position
label = [[UILabel alloc] initWithFrame:CGRectMake(10, offset, 30, 30)];
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat: #"%i",x+1];
label.textAlignment = UITextAlignmentCenter;
[tables[a] addSubview:label];
[label release];
NSLog(#"7 %# \n\n %#",score_data,scores_data);
//Add name
label = [[UILabel alloc] initWithFrame:CGRectMake(55, offset, 190, 30)];
label.backgroundColor = [UIColor clearColor];
label.text = [score_data objectAtIndex:0];
[tables[a] addSubview:label];
[label release];
//Add score
label = [[UILabel alloc] initWithFrame:CGRectMake(55, offset, 225, 30)];
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat: #"%#",[score_data objectAtIndex:1]];
label.textAlignment = UITextAlignmentRight;
[tables[a] addSubview:label];
[label release];
NSLog(#"8");
}
//Add title
switch (a) {
case 0:
path = [[NSBundle mainBundle] pathForResource: #"todays_scores_title" ofType:#"png"];
break;
case 1:
path = [[NSBundle mainBundle] pathForResource: #"week_scores_title" ofType:#"png"];
break;
case 2:
path = [[NSBundle mainBundle] pathForResource: #"all_scores_title" ofType:#"png"];
break;
}
[data release];
data = [[NSData alloc] initWithContentsOfFile:path];
title_image = [[[UIImage alloc] initWithData:data] autorelease];
title_view = [[[UIImageView alloc] initWithImage: title_image] autorelease];
title_view.frame = CGRectMake(25 + 320*a, 15, 270, 75); //Reposition and resize
[online_scores_pages addSubview:title_view];
NSLog(#"9");
//Add table
score_table_border = [[UIImageView alloc] initWithImage: border_image];
score_table_border.frame = CGRectMake(320*a, 100, 320, 320); //Reposition
[online_scores_pages addSubview: tables[a]];
[online_scores_pages addSubview: score_table_border];
[score_table_border release];
}
[data release];
if(submission){
[header_image release];
[header_view release];
}
[background_image release];
[background_view release];
[self addSubview: online_scores_pages];
[online_scores_pages becomeFirstResponder];
Thank you for any help. It is much appreciated.
The only iEqual: that I see is in the series of tests:
if([[scores_data objectAtIndex:0] isEqual:#""]){
if([[scores_data objectAtIndex:1] isEqual:#""]){
if([[scores_data objectAtIndex:2] isEqual:#""]){
So looking back at scores_data I see:
MutableArray * scores_data = [[first_data componentsSeparatedByString: #"\n"] mutableCopy];
Is first_data a NSString? If so I'm suspecting that componentsSeparatedByString will return a array of strings. This means that when you do [scores_data objectAtIndex:?] you will get a string.
The isEqual: at this point then looks wrong. Instead of isEqual: you should probably use isEqualToString:.
However, you're seeing a problem with accessing a deallocated object. That implies an excessive number of releases and I can't see that here.
I don't like that call to "mutableCopy" that you are using. I'd prefer to be more explicit in the initialisation:
MutableArray * scores_data = [[NSMutableArray alloc] initWithArray:[first_data componentsSeparatedByString: #"\n"]];
Finally! I found the problem.
I had this call - [background_view release]; once to many. I remove it at the end as it was being released when necessary in the for loop.
Now it works!!!!