setNeedsDisplay doesn't clean the window from old stuff

Discussion in 'iOS Programming' started by Nicsoft, Oct 17, 2009.

  1. macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #1
    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
    Code:
    //
    //  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
    
    Code:
    //
    //  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
    
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    Oct 17, 2009
    Location:
    Stockholm
    #3
    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
     

Share This Page