Setting the text on a UILabel is crashing my app - iphone

So I have setup my UILabel, but when I set the text in my game loop to a string that is the score (so the Text is reset every loop) my app crashes.
This is the error I get:
0x15560b0: cmpl (%eax), %ecx Thread 1
The Breakpoint says this:
EXC_BAD_ACCESS(code=1, address=0x67b30064
Here is how I am setting up my UILabel (in my init method) :
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
scoreString = [NSString stringWithFormat:#"%d", score];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//[scoreLabel setText: scoreString];
Thank You!

Are you doing your label update off the main queue? I had these crashes until I dispatched the update on the main queue.
dispatch_async(dispatch_get_main_queue(), ^{
self.lblSyncProgress.text = #"Some string";
});

//in your .h file
UILabel *scoreLabel;
NSString *scoreString;
//in your .m file
//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
[scoreLabel setFont: [UIFont fontWithName: #"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//In your update method
scoreString = [NSString stringWithFormat:#"%d", score];
scoreLabel.text = scoreString;

I noticed in your code that you had
scoreString = [NSString stringWithFormat:#"%d", score];
Make sure that "score" is a char. If it is not have a look at this

Related

How to add multiple label into an imageview?

I am new to this iphone development .I am now stuck with a problem .In my application i require coverflow in which multiple labels should be shown.So for that i added an image as a sub view for view and then added the labels as a sub view for the image view.Every thing works fine for the first 7 elements But when i scroll it is showing the same elements again
I am using
https://github.com/nicklockwood/iCarousel
this to attain coverflow
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIImageView *imageView =nil;
UILabel *yourLabel=nil;
UILabel *yourLabel1=nil;
UILabel *yourLabel2=nil;
cardAvailableDataObject *singleCard=[self.cards objectAtIndex:index];
if (view == nil)
{
//set up reflection view
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)] autorelease];
imageView = [ [ UIImageView alloc ] initWithFrame:CGRectMake(0,0,200,self.view.frame.size.width/1.42)];
[view addSubview:imageView];
yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -5, 200, 50)];
yourLabel.textAlignment = UITextAlignmentCenter;
[yourLabel setTextColor:[UIColor colorWithRed:(38/255.f) green:(171/255.f) blue:(226/255.f) alpha:1.0f]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 17.0f]];
[imageView addSubview:yourLabel];
yourLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 300, 20)];
[yourLabel1 setTextColor:[UIColor blackColor]];
[yourLabel1 setBackgroundColor:[UIColor clearColor]];
[yourLabel1 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel1];
yourLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(90, 61, 300, 20)];
[yourLabel2 setTextColor:[UIColor blackColor]];
[yourLabel2 setBackgroundColor:[UIColor clearColor]];
[yourLabel2 setFont:[UIFont fontWithName: #"Trebuchet MS" size: 12.0f]];
[imageView addSubview:yourLabel2];
}
else
{
imageView = [[view subviews] lastObject];
yourLabel = [[imageView subviews] lastObject];
yourLabel1 = [[imageView subviews] lastObject];
yourLabel2= [[imageView subviews] lastObject];
}
yourLabel.text=singleCard.name;
yourLabel2.text=singleCard.companyname;
yourLabel1.text=singleCard.designation;
imageView.image=[profile objectAtIndex:index];
return view;
}
This is my code .Can any one suggest how to solve this

iphone: how to code different label for each image in scrollview

I successfully created a UIscrollview (horizontal) for 50 images and also created a label of "1 of 50" page number displaying at the top of the image along with the scroll view(for example user scroll to next image it would show 2 of 50, 3 of 50, so forth). I am able to display a label at the bottom of the image as a description. My problem is how can I create or code to display different description for each image? Im thinking of NSarray but it shows some random numbers. Is there a sample I can look at to see what I am doing wrong. you can see the description part where I am lost from there. Thanks =)
CGFloat xOffset = 0;
NSInteger pageNumber = 1;
NSUInteger i;
for (i = 1; i <= 5; i++)
{
NSString *imageName = [NSString stringWithFormat:#"creative%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(xOffset, 0, 320, 288.5)];
[ourScrollView addSubview:imageView];
[imageView release];
UILabel *pageNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, -10, 320, 40)];
[pageNumberLabel setText:[NSString stringWithFormat:#"%d of 5", pageNumber]];
[pageNumberLabel setTextAlignment:UITextAlignmentLeft];
[pageNumberLabel setTextColor:[UIColor whiteColor]];
[pageNumberLabel setBackgroundColor:[UIColor clearColor]];
[pageNumberLabel setFont:[UIFont fontWithName:#"MarkerFelt-Thin" size:18]];
[ourScrollView addSubview:pageNumberLabel];
[pageNumberLabel release];
NSArray *description = [NSArray arrayWithObjects:#"wow", #"bam", #"pow", #"zing", #"bling",nil];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset, 250, 320, 40)];
[descriptionLabel setText:[NSString stringWithFormat:#"%#", description]];
[descriptionLabel setTextAlignment:UITextAlignmentLeft];
[descriptionLabel setTextColor:[UIColor whiteColor]];
[descriptionLabel setBackgroundColor:[UIColor clearColor]];
[ourScrollView addSubview:descriptionLabel];
[descriptionLabel release];
xOffset = xOffset + 320;
pageNumber = pageNumber + 1;
You're setting the descriptionLabel with a format of #"%d", which is a decimal format. What is "description"? If it's a NSString then you should be using #"%#".

Memory leaks in UIView class

I have created customize UIView with following code .....it's running with no issue....but when i check for Memory leaks...it shows leaks at point where i have made comment ...this is subclass of Uiview.
Please help me...
-(id) initWithLabel:(NSString*)labelName{
self = [super init];
if (self) {
self.layer.cornerRadius=8.0f;
self.layer.borderColor=[UIColor whiteColor].CGColor;
self.layer.borderWidth=2.0f;
/*memory leaks 28.6% */ [self setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
[self setFrame:CGRectMake(86, 168, 160, 77)];
[self setAlpha:1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 28, 75, 21)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextAlignment:UITextAlignmentLeft];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont fontWithName:#"Helvetica" size:17]];//memory leak 57%
[label setText:labelName];
[label setOpaque:NO];//memory leak
[self addSubview:label];
[label release];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(108, 22, 37, 37)];
[activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator setBackgroundColor:[UIColor clearColor]];
[activityIndicator setHidesWhenStopped:YES];
[self addSubview:activityIndicator];
[activityIndicator release];
}
return self;
}
try this
UIColor *myColor = [[UIColor alloc] initWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1];
[self setBackgroundColor:myColor];
[myColor release];
Hey man that is not memory leaks. that is autoreleased objects.so dont worry about them.apple wont reject urs.the condition is if u use alloc,init,new or retain then only u have responsibility to release.so leave it.otherwise do like above by alloc and init then release it .that'st it

UINavigationBar title

I am trying to use a UILabel to replace the title at the UINavigationBar, the code is as follows:
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
The problem is that, how do I remove the original title of the bar? I didn't declare any self.title = #"title", but it always shows it there:
If I do self.title = nil, then everything is gone... How do eliminate this mysterious title from the navbar and just use the UILabel I created.
Why don't you just do self.title = #""?
EDIT: Try this?
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor blackColor]];
UILabel * nav_title = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 220, 25)];
nav_title.font = [UIFont fontWithName:#"Arial-BoldMT" size:18];
nav_title.textColor = [UIColor whiteColor];
nav_title.adjustsFontSizeToFitWidth = YES;
nav_title.text = title;
self.title = #"";
nav_title.backgroundColor = [UIColor clearColor];
[bar addSubview:nav_title];
[nav_title release];
Use self.navigationItem.titleView = nav_title; instead of adding your label as a subview.
Use this:
UILabel *label = [[UILabel alloc]init];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:self.title];
label.adjustsFontSizeToFitWidth=YES;
label.lineBreakMode=UILineBreakModeWordWrap;
label.numberOfLines=0;
[label setFont:[UIFont boldSystemFontOfSize:16.0]];
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
Hope this will help u..!

tableview cell load all at once in iphone

I have implemented following code in my application.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%#%i",searchQueryString,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
XML_SearchResult *t=[arrayResults objectAtIndex:indexPath.row];
cell=((indexPath.row%2)==0) ?
[self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:NO] :
[self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:YES] ;
CGRect a=[cell frame];
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
bImg.image=[UIImage imageNamed:#"white-rect-tab-3.png"];
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.selectedBackgroundView=bImg; [bImg release];
}
return cell;
}
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier drillTitle:(NSString*)drillTitle stroke_type:(NSString*)stroke_type exercise_type:(NSString*)exercise_type level_name:(NSString*)level_name age_range:(NSString*)age_range alterNate:(BOOL)alterNate
{
CGRect label1Frame=CGRectMake(5, 20, 260, 32),label2Frame=CGRectMake(5, 62, 120, 30),label3Frame=CGRectMake(130, 62, 170, 30);//,BgFrame=CGRectMake(0, 0, 320, 60);
UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 312, 95) reuseIdentifier:cellIdentifier] autorelease];
UILabel *tmp; UIImageView *imgBg;
cell.backgroundColor=[UIColor clearColor];
// drill title label
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 1, 260, 32)];
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:18]]; tmp.text=drillTitle; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// title label - tip
tmp=[[UILabel alloc] initWithFrame:label1Frame];
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:18]]; tmp.text=stroke_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// sub title - exercise type label - tip
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 40 , 260, 32)];
tmp.textColor=[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:#"ArialRoundedMTBold" size:15]]; tmp.text=exercise_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// Age - Range
tmp=[[UILabel alloc] initWithFrame:label2Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:#"Age Range : %#",age_range];
tmp.textColor= [UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];// : [UIColor blackColor] ;
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:#"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)]; [tmp setShadowColor:[UIColor lightGrayColor]];
[cell.contentView addSubview:tmp]; [tmp release];
// Difficulty leve - Range
tmp=[[UILabel alloc] initWithFrame:label3Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:#"| Diff. Level : %#",level_name];
tmp.textColor=[UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];
[tmp setShadowColor:[UIColor lightGrayColor]];
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:#"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)];
[cell.contentView addSubview:tmp]; [tmp release];
// arrow
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(280, 35, 13, 13)]; imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:#"arrow-2.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release];
// line
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(-4, 94, 298, 2)]; imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:#"line.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
return cell;
}
That's all about customizing cell dynamically.
But the problem is - first time only visible cells are created, then after - user tries to scroll down. other cells which are going to be visible is going to be created & then displayed. Due to this, there is some delay in scrolling tableview at first time.
What is the solution for this ?
When developing for mobile platforms, you need to be concerned about memory footprints. I feel that if you tried to load 400 cells with images all at once, you might be asking for trouble. It's not the recommended method and you will certainly introduce a huge delay in loading this.
Instead of hacking like this, work in improving the performance of your cellForIndexPath function to make it load quicker. Or asynchronously.
It is probably the image loading which takes all the time. Creating cells should not take very long, especially if you reuse old ones.
If the images are often the same, then you could cache those. Also scaling takes time and also if the images are not PNG they will take longer to load.
But the solution for me has been to not load the images when I create the cell, i.e. NOT in:
tableView:cellForRowAtIndexPath:
but instead implement:
scrollViewDidEndDecelerating
This is a delegate function which is called when the table stops scrolling. Then you can get a list of the visible cells (indexPathsForVisibleRows) and set the image on each of those.