PDA

View Full Version : setNeedsDisplay doesn't clean the window from old stuff




Nicsoft
Oct 17, 2009, 05:40 AM
Hello,

I am totally new and are trying to learn how to create iPhone apps.

When learning how to update the screen, I am trying to move a NSString around, but it multiplies and is positioned in different places. Can anyone tell me what I am not understanding? I am also an old Java guy, perhaps I am not understanding the language correctly...

Please see code below.

Thanks in advance!

Cheers,
Niklas
//
// MyView.m
// HelloWorld
//
// Created by nic on 2009-10-13.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MyView.h"
#import "TextClass.h"


@implementation MyView


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
location = CGPointMake(40, 20);
id textObject = [[TextClass alloc] init];
font = [UIFont systemFontOfSize:24];
hello = [textObject getText];
hello = [textObject getText];
}

return self;
}

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
location = point;
[self setNeedsDisplay];
return self;

}

- (void)drawRect:(CGRect)rect {

CGPoint location2 = CGPointMake(10, 40);
[[UIColor whiteColor] set];
[hello drawAtPoint:location withFont:font];
NSString *l = [NSString stringWithFormat:@"%d", [hello length]];
[l drawAtPoint:location2 withFont:font];

}


- (void)dealloc {
[super dealloc];
}


@end


//
// MyView.h
// HelloWorld
//
// Created by nic on 2009-10-13.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

CGPoint location;
NSString *hello;
UIFont *font;


@interface MyView : UIView {

}

@end



robbieduncan
Oct 17, 2009, 06:33 AM
If you need the entire view cleared/initialised to a solid colour you have to do it: setNeedsDisplay: just tells the OS that you need to update the view.

Nicsoft
Oct 17, 2009, 07:05 AM
Hello, Thanks for your quick answer!

Ok I see. I tried to brows around finding out how to do this. Couldn't find any really good info. Is the strategy to use setNeedsDisplayRect and define the entire screen? I couldn't find any helpfull method for the UIView class. Or should I create a new instance of MyView?

Thanks again!

/Niklas