Sorry guy, I really don't understand it......
I will post all the source code here, hope someone can help me,
I'm using a NSTimer to create a SubView from MainView every second,
after create SubView, I want SubView to change the count +1 inside MainView,
but I can't get it, the [mainView counter] always return 1,
please help......
MainView.h
#import <UIKit/UIKit.h>
@class SubView;
@interface MainView : UIView {
SubView *subView;
NSTimer *timer;
float counter;
}
@property (nonatomic, retain)SubView *subView;
@property (nonatomic, retain)NSTimer *timer;
@property (readwrite)float counter;
-(float)counter;
-(void)addView;
@end
MainView.m
#import "MainView.h"
#import "SubView.h"
@implementation MainView
@synthesize timer, subView;
- (id)init
{
if(self == [super init])
{
[self setCounter:1];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(addView) userInfo:nil repeats:YES];
}
-(float)counter{
return counter;
}
- (void)setCounter:(float)input {
counter += input;
}
- (void)addView {
subView = [[SubView alloc] initWithFrame:[self bounds]];
[self addSubview:subView];
}
- (void)dealloc {
[super dealloc];
}
@end
SubView.h
#import <UIKit/UIKit.h>
@class MainView;
@interface SubView : UIView {
MainView *mainView;
}
@property (nonatomic, retain)MainView *mainView;
@end
SubView.m
#import "SubView.h"
#import "MainView.h"
@implementation SubView
@synthesize mainView;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
mainView = [[MainView alloc] init];
NSLog(@"%f",[mainView counter]);
}
return self;
}
- (void)dealloc {
[super dealloc];
}
@end