Jump to content

Help! Iphone memory problem


1 post in this topic

Recommended Posts

I had just create a simple app.

 

the main window will addSubview (uiview)ball,

 

when u touch the ball, the touchBegan will call [self removeFromSuperView];

 

and delegate window to add (uiview)ball again.

 

After pressing few time on the ball, the touchBegan event will not working,

 

What's the problem? Is that about memory?

 

here is my 4 files:

 

SpeedTestAppDelegate.h

#import <UIKit/UIKit.h>

@class Ball;

@interface SpeedTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Ball *ball;
NSTimer *timer;
float count;
float loop;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) Ball *ball;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic) float count;

- (void)onEnterFrame;

@end

 

SpeedTestAppDelegate.m

#import "SpeedTestAppDelegate.h"
#import "Ball.h"

@implementation SpeedTestAppDelegate

@synthesize window, ball, timer, count;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
count = 0;
loop = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onEnterFrame) userInfo:nil repeats:YES];

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after app launch	
[window makeKeyAndVisible];


}

- (void)onEnterFrame {
if(count < 2 )
{
	NSLog(@"total subviews = %d, loop time = %f", window.subviews.count, loop);
	ball = [[Ball alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 80.0)];
	[window addSubview:ball];
	[ball release];
	count++; 
	loop++;
}
}

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

@end

 

Ball.h

 

#import <UIKit/UIKit.h>

@class SpeedTestAppDelegate;

@interface Ball : UIView {
UIImageView *image;
NSTimer *timer;
float speed;
SpeedTestAppDelegate *appDeleagte;
}

@property (nonatomic, retain)UIImageView *image;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)SpeedTestAppDelegate *appDeleagte;

- (void)onEnterFrame;

@end

 

Ball.m

#import "Ball.h"
#import "SpeedTestAppDelegate.h"

@implementation Ball

@synthesize image, timer, appDeleagte;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
	[self setBackgroundColor:[UIColor clearColor]];
	speed = 10;
	timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onEnterFrame) userInfo:nil repeats:YES];
}
return self;
}

- (void)drawRect:(CGRect)rect {
float x = rand() % 240;
float y = rand() % 480;
CGRect f = self.frame;
f.origin.x = x;
f.origin.y = y;
self.frame = f;

image = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 80.0)];
image.image = [UIImage imageNamed:@"ball.png"];
[self addSubview:image];
[image release];
}

- (void)onEnterFrame {
CGRect f = self.frame;
f.origin.x += speed;
self.frame = f;

if(f.origin.x < 0 || f.origin.x > 240) {
	speed *= -1;
}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
appDeleagte = (SpeedTestAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDeleagte setCount:1.0];
[self removeFromSuperview];
}

- (void)dealloc {
[appDeleagte release];
[timer release];
[super dealloc];
}

@end

Link to comment
Share on other sites

 Share

×
×
  • Create New...