Count and remove Subviews from view in ios - iphone

this is the way I create and add subviews into a view.
I'm wondering why the count always returns 0, when it should return "hundreds". What am I doing wrong, thanks!
I have added more code where shows clearly my issue. I copy/pasted all functions involved on my initial question.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.contenedor addSubview:vistaPanelBotones];
[self crearBotones];
}
- (void) crearBotones {
UIColor *colores[] = {
[UIColor blueColor],
[UIColor brownColor],
[UIColor redColor],
[UIColor orangeColor],
[UIColor greenColor],
[UIColor yellowColor],
[UIColor purpleColor],
[UIColor blackColor],
[UIColor whiteColor],
[UIColor darkGrayColor],
[UIColor magentaColor],
[UIColor cyanColor],
};
int indice = 0;
for (int col = 0; col < self.vistaPanelBotones.frame.size.width ; col=col+20) {
for (int fila = 0; fila < self.vistaPanelBotones.frame.size.height-20 ; fila = fila+20) {
CGRect frame = CGRectMake(col, fila, 20, 20);
Boton *boton = [Boton new];
boton.frame = frame;
boton.layer.backgroundColor = colores[(fila + col) % 7].CGColor;
boton.layer.cornerRadius = 0.25;
boton.layer.borderWidth = 0.25;
boton.layer.borderColor = [UIColor whiteColor].CGColor;
boton.layer.delegate = self;
[self.vistaPanelBotones addSubview:boton];
[boton setNeedsDisplay];
}
indice++;
}
NSLog(#"Vista Botones SubViews:%i",[[self.vistaPanelBotones subviews] count]);
}
- (IBAction)reiniciar:(id)sender {
if (self.vistaPanelBotones == nil){
NSLog(#"no existe la vista");
}
NSUInteger count = self.vistaPanelBotones.subviews.count;
NSLog(#"Vista SubViews: %i",count);
}

Here are a few of my thoughts:
I would check if self.vistaPanelBotones is non-nil, just in case (if it were nil, you would not get any errors in that code, but also no subviews).
Possibly executing this before you have a valid frame (IIRC, viewWillAppear is the earliest callback with valid geometry)
I'm pretty sure if boton was nil you would get an exception when adding as a subview, but it's another test worth using for debugging.

Use [[self.vistaPanelBotones subviews] count] to count the number of subviews, but there is an elegant way to remove all subviews from a view in Objective-C. Try this:
[[self.vistaPanelBotones subviews] makeObjectsPerformSelector:#selector(removeFromSuperView];

Related

Create a transparent Calendar with Tapku Calendar

First I wanna say thanks for all the helpful answers on this site. I starting programming about six months ago and many of the things I've learned have been from questions/answers here.
I'm using the Calendar from Tapku Library in an iPhone project and want the calendar tiles to be transparent so that I can see the view behind my TKCalendarMonthView view.
I implemented the TKCalendarMonthView using the code from this tutorial by Benjamin Pearson.
Then I removed the tile images and tried code from this answer by #Jacques, so the drawrect function in TKCalendarMonthView.m looks like this:
- (void) drawRect:(CGRect)rect {
//From Jacques' StackOverflow answer (I also put this in the init)
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
//From Jacques' answer
[[UIColor clearColor] setFill
];
UIRectFill(rect);
//Remove CGContextRef
//CGContextRef context = UIGraphicsGetCurrentContext();
//UIImage *tile = [UIImage imageWithContentsOfFile:TKBUNDLE(#"TapkuLibrary.bundle/Images/calendar/Month Calendar Date Tile.png")];
CGRect r = CGRectMake(0, 0, 46, 44);
//From Jacques' StackOverflow answer
[[UIColor clearColor] setFill];
UIRectFill(r);
//Remove this sense we won't use the tile image
//CGContextDrawTiledImage(context, r, tile.CGImage);
if(today > 0){
int pre = firstOfPrev > 0 ? lastOfPrev - firstOfPrev + 1 : 0;
int index = today + pre-1;
CGRect r =[self rectForCellAtIndex:index];
r.origin.y -= 7;
//Don't use image here
//[[UIImage imageWithContentsOfFile:TKBUNDLE(#"TapkuLibrary.bundle/Images/calendar/Month Calendar Today Tile.png")] drawInRect:r];
}
int index = 0;
UIFont *font = [UIFont boldSystemFontOfSize:dateFontSize];
UIFont *font2 =[UIFont boldSystemFontOfSize:dotFontSize];
//Change the font for our dates:
font = [UIFont fontWithName:#"HelveticaNeue-Light" size:dateFontSize];
font2 = [UIFont fontWithName:#"HelveticaNeue-Light" size:dateFontSize];
UIColor *color = [UIColor grayColor];
if(firstOfPrev>0){
[color set];
for(int i = firstOfPrev;i<= lastOfPrev;i++){
r = [self rectForCellAtIndex:index];
if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
index++;
}
}
//Set the color for all dates in the current month that are not today
color = [UIColor colorWithRed:59/255. green:73/255. blue:88/255. alpha:1];
[color set];
for(int i=1; i <= daysInMonth; i++){
r = [self rectForCellAtIndex:index];
if(today == i) [[UIColor whiteColor] set];
if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
if(today == i) [color set];
index++;
}
[[UIColor grayColor] set];
int i = 1;
while(index % 7 != 0){
r = [self rectForCellAtIndex:index] ;
if ([marks count] > 0)
[self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
else
[self drawTileInRect:r day:i mark:NO font:font font2:font2];
i++;
index++;
}
}
The problem is that now the tiles (CGRects) are black; or whatever view is directly behind the them is black, and, frankly, I'm a bit lost in Tapku's code. Does anyone know why the tiles are black? Or where in the Tapku code I should be looking? I'm not very familiar with Core Graphics, so maybe I'm missing something basic/obvious.
NOTE: I also tried changing the color of TKCalendarMonthView's tileBox (which is a UIScrollView that seems to contain the calendar tiles) and although it did change color it didn't effect the tiles' background color.
Thanks in advance! And please let me know if any of this is unclear.
You can transparent calendar tiles by following steps
Step-1
Comment the code in TKCalendarMonthView.m
+ (void) initialize{
if (self == [TKCalendarMonthTiles class]){
//tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(#"calendar/Month Calendar Date Tile.png")];
}
}
Step-2
Change the code in TKCalendarMonthView.m
add line of code [self.currentTile setBackgroundColor:[UIColor clearColor]];
before the line [self.tileBox addSubview:self.currentTile];
in method - (void) _setupCurrentTileView:(NSDate*)date
so your code look like
- (void) _setupCurrentTileView:(NSDate*)date{
....
[self.currentTile setBackgroundColor:[UIColor clearColor]];
[self.tileBox addSubview:self.currentTile];
....
}
Finally I get how to make the background color clear in calender view.
Change code as below.
In TKCalendarMonthView.m
(1) Comment/remove below line.
+ (void) initialize
{
if (self == [TKCalendarMonthTiles class])
{
//tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(#"calendar/Month Calendar Date Tile.png")]; COMMENT THIS LINE
}
}
(2) In - (void) _setupCurrentTileView:(NSDate*)date method Add [self.currentTile setBackgroundColor:[UIColor clearColor]]; line as below
- (void) _setupCurrentTileView:(NSDate*)date{
if(self.currentTile) return;
NSDate *month = [date firstOfMonthWithTimeZone:self.timeZone];
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:month startOnSunday:self.sunday timeZone:self.timeZone];
NSArray *data = [self.dataSource calendarMonthView:self marksFromDate:dates[0] toDate:[dates lastObject]];
self.currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:month marks:data startDayOnSunday:self.sunday timeZone:self.timeZone];
[self.currentTile setTarget:self action:#selector(_tileSelectedWithData:)];
[self.currentTile setBackgroundColor:[UIColor clearColor]]; // ADD THIS LINE
[self.tileBox addSubview:self.currentTile];
self.monthYear.text = [date monthYearStringWithTimeZone:self.timeZone];
[self _updateSubviewFramesWithTile:self.currentTile];
}

UIAccessibility VoiceOver announces wrong page number for UIScrollView

I am trying to make an existing app as accessible as possible for voice over.
Currently, I have a uiviewcontroller thats basically a paging photo view with a uipagecontrol below the uiscrollView (tourScrollView) that indicates the current image/page being viewed.
here's the code that calculate's the current page:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGFloat pageWidth = scrollView.frame.size.width;
self.tourScrollView.isAccessibilityElement = NO;
scrollView.isAccessibilityElement = NO;
int currentPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = currentPage;
}
the page calculation code works perfect.
There are a total of 5 images being shown.
With voice over enabled, when the scroll view scrolls, instead of going
page 1 of 5
page 2 of 5
page 3 of 5
page 4 of 5
page 5 of 5
it goes like this.
page 1 of 6
page 2 of 6
page 3 of 6
page 5 of 6
page 6 of 6
Here's the code where the images are added to the scrollView
-(void)addImagesToScrollview{
NSArray *welcomeImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"img-01.png"],
[UIImage imageNamed:#"img-02.png"],
[UIImage imageNamed:#"img-03.png"],
[UIImage imageNamed:#"img-04.png"],
[UIImage imageNamed:#"img-05.png"],nil];
CGRect scrollViewFrame = tourScrollView.frame;
CGFloat scrollViewWidth = scrollViewFrame.size.width;
CGFloat scrollViewHeight = scrollViewFrame.size.height;
CGFloat imageX;
for (int i = 0; i<[welcomeImages count]; i++) {
int index = i;
imageX = (scrollViewWidth*index) + (scrollViewWidth - IMAGE_WIDTH)/2.0;
CGRect boarderViewRect = CGRectMake(imageX, 20.0f, IMAGE_WIDTH, IMAGE_HEIGHT);
UIView *whiteBorderView = [[UIView alloc] initWithFrame:boarderViewRect];
whiteBorderView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[welcomeImages objectAtIndex:i]];
CGRect imageRect = CGRectInset(boarderViewRect, IMAGE_INSET, IMAGE_INSET);
imageView.frame = imageRect;
CGRect descriptionRect = CGRectMake((scrollViewWidth*index) + 20.0f, imageRect.origin.y + imageRect.size.height+10, 280, 90);
CGSize maximumLabelSize = CGSizeMake(descriptionRect.size.width,120);
descriptionRect.size = [[self descriptionForIndex:i] sizeWithFont:[UIFont systemFontOfSize:16.0] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation];
UILabel *imageDescription = [[UILabel alloc] initWithFrame:descriptionRect];
imageDescription.text = [NSString stringWithFormat:#"%#",[self descriptionForIndex:i]];
imageDescription.numberOfLines = 0;
imageDescription.backgroundColor = [UIColor clearColor];
imageDescription.font = [UIFont systemFontOfSize:16.0];
imageDescription.textColor = [UIColor colorWithRed:(119.0/255.0) green:(119.0/255.0) blue:(119.0/255.0) alpha:1.0];
imageDescription.textAlignment = UITextAlignmentCenter;
imageDescription.shadowColor = [UIColor whiteColor];
imageDescription.shadowOffset = CGSizeMake(0,1);
[tourScrollView addSubview:whiteBorderView];
[tourScrollView addSubview:imageView];
[tourScrollView addSubview:imageDescription];
if (i == [welcomeImages count]-1) {
tourScrollView.contentSize = CGSizeMake(imageView.frame.origin.x + scrollViewWidth -((scrollViewWidth - IMAGE_WIDTH)/2.0), scrollViewHeight);
}
}
}
I'd appreciate if someone points me to the right direction to make voice over say the correct page numbers.
update: Enabling/disabling pagingEnabled makes no difference. I think voiceOver overrides the paging calculations I do based on the scrollview size.
Here's what fixed it:
removed the commented code, and added a line for the content size outside the for loop
-(void)addImagesToScrollview{
NSArray *welcomeImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"img-01-welcome.png"],
[UIImage imageNamed:#"img-02-welcome.png"],
[UIImage imageNamed:#"img-03-welcome.png"],
[UIImage imageNamed:#"img-04-welcome.png"],
[UIImage imageNamed:#"img-05-welcome.png"],nil];
CGRect scrollViewFrame = tourScrollView.frame;
CGFloat scrollViewWidth = scrollViewFrame.size.width;
CGFloat scrollViewHeight = scrollViewFrame.size.height;
CGFloat imageX;
for (int i = 0; i<[welcomeImages count]; i++) {
int index = i;
imageX = (scrollViewWidth*index) + (scrollViewWidth - IMAGE_WIDTH)/2.0;
CGRect boarderViewRect = CGRectMake(imageX, 20.0f, IMAGE_WIDTH, IMAGE_HEIGHT);
UIView *whiteBorderView = [[UIView alloc] initWithFrame:boarderViewRect];
whiteBorderView.backgroundColor = [UIColor whiteColor];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[welcomeImages objectAtIndex:i]];
CGRect imageRect = CGRectInset(boarderViewRect, IMAGE_INSET, IMAGE_INSET);
imageView.frame = imageRect;
CGRect descriptionRect = CGRectMake((scrollViewWidth*index) + 20.0f, imageRect.origin.y + imageRect.size.height+10, 280, 90);
CGSize maximumLabelSize = CGSizeMake(descriptionRect.size.width,120);
descriptionRect.size = [[self descriptionForIndex:i] sizeWithFont:[UIFont systemFontOfSize:16.0] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeTailTruncation];
UILabel *imageDescription = [[UILabel alloc] initWithFrame:descriptionRect];
imageDescription.text = [NSString stringWithFormat:#"%#",[self descriptionForIndex:i]];
imageDescription.numberOfLines = 0;
imageDescription.backgroundColor = [UIColor clearColor];
imageDescription.font = [UIFont systemFontOfSize:16.0];
imageDescription.textColor = [UIColor colorWithRed:(119.0/255.0) green:(119.0/255.0) blue:(119.0/255.0) alpha:1.0];
imageDescription.textAlignment = UITextAlignmentCenter;
imageDescription.shadowColor = [UIColor whiteColor];
imageDescription.shadowOffset = CGSizeMake(0,1);
[tourScrollView addSubview:whiteBorderView];
[tourScrollView addSubview:imageView];
[tourScrollView addSubview:imageDescription];
// if (i == [welcomeImages count]-1) {
// tourScrollView.contentSize = CGSizeMake(imageView.frame.origin.x + scrollViewWidth -((scrollViewWidth - IMAGE_WIDTH)/2.0), scrollViewHeight);
// }
}
tourScrollView.contentSize = CGSizeMake(320.0*[welcomeImages count], scrollViewHeight);
}
I am still not sure why VO would mess up the page numbers. The scrollview behavior, before and after the fix is still the same (bouncing and paging work identical).
will update the answer if I get to know more about this issue.
One easy option is to conform to your UIScrollViewDelegate to UIScrollViewAccessibilityDelegate and return the desired string.
For example:
extension MyScrollViewDelegate: UIScrollViewAccessibilityDelegate {
func accessibilityScrollStatus(for scrollView: UIScrollView) -> String? {
"\(self.currentPage) of \(self.pageCount)"
}
}

how to programmatically draw border and remove it after words around UIImageview?

Please read my question before marking it as duplicate or repeat question...
I have one scroleView in which I put some images in tabular form.
When user click one of it the next view controller is shown and the
clicked image's uiview get green border.
And when user navigate back to this view the clicked image is shown
with green border. all this worked fine
But the problem starts when user clicks other images : the previously clicked image doesn't get back to normal, ie, its border stays there even if I put its width to 0.0 and color to clearColor
Please guide me how to remove those borders
My code is as below:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [[UIView alloc] initWithFrame:frame];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:#"add"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[fr addGestureRecognizer:tapGesture];
[fr addSubview:imgView];
fr.tag = index;
[self.scrollDisplay addSubview:fr];
[self.scrollDisplay bringSubviewToFront:fr];
}
}
}
[self.view addSubview:self.scrollDisplay];
This method is called in viewWillAppear:animated: method
Edit
after some navigation forth and back
New Answer- I think the problem is that you're adding new views again and again. Instead of doing UIView *fr = [[UIView alloc] initWithFrame:frame];, find the already existing view as: UIView *fr = [self.scrollDisplay viewWithTag:index]; and make changes to it. In effect, new imageviews are being added over the old ones which is highly inefficient, besides causing the mentioned issue. I'm assuming you've already added the views to scrollDisplay. You also shouldn't create new imgView objects again. Set tags for them that make them unique and easy to get. For eg, set the tag 999 for each imgView and retrieve it as:UIImageView *imgView = [fr viewWithTag:999]; Also, get rid of [fr addSubview:imgView];, [self.scrollDisplay addSubview:fr]; and [self.view addSubview:self.scrollDisplay]; towards the end. So your code should look like this:
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < 2; ++col)
{
int index = (row * 2) + col;
if(index < [tempArr count])
{
CGRect frame = CGRectMake(10+col*(10+145),10+row*(5+100),145, 100);
UIView *fr = [self. scrollDisplay viewWithTag:index];
CGRect imgFrame = CGRectMake(0, 0, 145, 100);
UIImageView *imgView = [fr viewWithTag:999];
//imgView.image = [UIImage imageNamed:[[tempArr objectAtIndex:index]valueForKey:#"add"]]; //<--You've already set the image before so you don't need this
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapAction:)];
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
if(selctedFrame == index)//Here i put border
{
fr.layer.borderWidth = 2.0f;
fr.layer.borderColor = [UIColor greenColor].CGColor;
}
else //here i remove them
{
fr.layer.borderWidth = 0.0f;
fr.layer.borderColor = [UIColor clearColor].CGColor;
}
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
//[fr addGestureRecognizer:tapGesture]; //<-- Not needed if you've already done this
//[fr addSubview:imgView];//<-- Not needed
fr.tag = index;
//[self.scrollDisplay addSubview:fr];//<-- Not needed
[self.scrollDisplay bringSubviewToFront:fr];//<-- probably not needed
}
}
}
//[self.view addSubview:self.scrollDisplay];//<-- Not needed
i just need to do is to set the border color to the background color of the view. any other options did nothing the border is not been hidden through any thing else the only work around i got is this

How to scroll characters on a label in Cocos2D?

I have a sprite on which there is a label (CCLabelTTF). On that label i have A,B,C,D,E, etc printed when they are clicked. I want them to scroll left. I've googled some tuorials but i am unable to find the solution and stuck here for long. Here is the screenshot of my game. You can see the characters from A to J. When i click on more incoming characters, that portion should scroll. What can i do to make the characters scroll?
Here is the code from which the characters are shown on label (lblSelectedAlpha) added on sprite:-
-(void)showSelectedAlphaBet{
fSelectedAlphaY =26;
if (tempBackground) {
[tempBackground removeFromParentAndCleanup:YES];
}
tempBackground = [CCSprite spriteWithFile:#"stripe_2.png"];
tempBackground.position = ccp(160,30);
[self addChild:tempBackground z:30];
for (int i=0; i<[arryAddSelectedChar count]; i++) {
// NSLog(#"[arryAddSelectedChar count] %#",[arryAddSelectedChar objectAtIndex:i]);
lblSelectedAlpha = [CCLabelTTF labelWithString:
[arryAddSelectedChar objectAtIndex:i]dimensions:CGSizeMake(30,30)
alignment:UITextAlignmentCenter fontName:#"Marker Felt" fontSize:30];
lblSelectedAlpha.position = ccp(fSelectedAlphaY,25);
lblSelectedAlpha.tag=i;
lblSelectedAlpha.color = ccc3(125,125,125);
[tempBackground addChild:lblSelectedAlpha z:5];
fSelectedAlphaY +=25;
}
}
Scrolling is simply a constant change in position over time. Basically something like this:
-(void) update:(ccTime)delta
{
label.position = CGPointMake(label.position.x - 1, label.position.y);
}
Well, this is how i implemented scrolling in my cocos2d game
// How to add scroll on label. This i did with a UITextView
// inside init, i took
{
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10,430, 150, 50)]
alphabetScroll = [[UITextView alloc] initWithFrame:CGRectMake(10,0,50 ,50)];
alphabetScroll.font = [UIFont fontWithName:#"verdana" size:30.0f];
alphabetScroll.backgroundColor = [UIColor clearColor];
alphabetScroll.textColor = [UIColor colorWithRed:125.0/255.0 green:125.0/255.0 blue:125.0/255.0 alpha:1.0f];
alphabetScroll.userInteractionEnabled=NO;
[scroll addSubview:alphabetScroll];
[[[CCDirector sharedDirector]openGLView]addSubview:scroll];
}
-(void)showSelectedAlphaBet
{
NSLog(#"showSelectedAlphaBet");
fSelectedAlphaY =26; // I have 26 alphabets
if (tempBackground) {
[tempBackground removeFromParentAndCleanup:YES];
}
// Area where alphabets are added on touch
tempBackground = [CCSprite spriteWithFile:#"stripe_2.png"];
tempBackground.position = ccp(160,30);
[self addChild:tempBackground z:30];
bottomAlphabet = #" ";
for (int i=0; i<[arryAddSelectedChar count]; i++) {
NSLog(#"[arryAddSelectedChar count] %#",[arryAddSelectedChar objectAtIndex:i]);
bottomAlphabet = [bottomAlphabet stringByAppendingString:[arryAddSelectedChar objectAtIndex:i]];
}
// Implementing shifting/scrolling
int newScrollViewWidth;
int newLabelWidth;
newScrollViewWidth =25*[arryAddSelectedChar count];
newLabelWidth =50*[arryAddSelectedChar count];
[scroll setContentSize:CGSizeMake(newScrollViewWidth, 45)];
alphabetScroll.frame = CGRectMake(0, 0, newLabelWidth, 45);
alphabetScroll.text = bottomAlphabet;
if (newScrollViewWidth > 150) {
CGPoint svos;
CGPoint pt;
svos = scroll.contentOffset;
CGRect rc = [alphabetScroll bounds];
rc = [alphabetScroll convertRect:rc toView:scroll];
pt = rc.origin;
pt.y = 0;
pt.x += 20*([arryAddSelectedChar count]-5);
[scroll setContentOffset:pt animated:YES];
}
}
// In dealloc
[alphabetScroll release];

Why is UIImage's drawAtPoint throwing an error in this code?

I am creating custom cell in my iphone aap and I am also adding an image to it but I continue to get error at [image drawatpoint:p];
the error states
*** -[UIImage drawAtPoint:]: message sent to deallocated instance 0x45cb1f0
my code is
-(void)setImage:(UIImage *)i
{
//[image release];
image = i;
[self setNeedsDisplay];
}
- (void)setNeedsDisplay
{
[super setNeedsDisplay];
[contentView setNeedsDisplay];
}
- (void)drawContentView:(CGRect)r
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor yellowColor];
UIColor *textColor = [UIColor blackColor];
if(self.selected)
{
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
CGContextFillRect(context, r);
CGPoint p;
p.x = 3;
p.y = 3;
[textColor set];
UIImage *image2 = image;
[image2 drawAtPoint:p];
p.x += 70; // space between words
[text2 drawAtPoint:p withFont:firstTextFont];
//[image release];
}
You do not retain the image in your setter (setImage:). As the error message says, it's gone at the time you try to draw it.