Jump to content

How to get different class var


14 posts in this topic

Recommended Posts

I had create 2 class MainView and subView,

 

when I working on subView, how can I get the MainView var?

 

because it always return 0.

 

When I working on other program, I will use static var,

 

but in obj-c, I don't know how to work on it.

 

 

MainView.h
@interface MainView:UIView {
float counter;
}
@property (nonatomic, retain)float counter;
-(float)getCounter;

MainView.m
@synthesize counter;
counter = 1;
-(float)getCounter {
return counter;
}

subView.h
@class MainView;
@interface subView:UIView {
MainView *mainView
}
@property (nonatomic, retain)MainView *mainView;

subView.m
#import "MainView.h"
@synthesize mainView;
mainView = [MainView alloc];	<--I think the problem is I create a new mainView
NSLog(@"%f", [mainView getCounter]);   <--here always return 0

Link to comment
Share on other sites

I try that but it's not working.......

 

am I need to add the init method inside the MainView class?

 

Two questions first:

1. Why do you need getCounter when you synthetized property?

2. Are you sure that this piece of code compiles properly, because you used

@property (nonatomic, retain)float counter;

One can not retain primitive types.

 

The code should look like this:

MainView.h
@interface MainView:UIView {
}
@property (nonatomic, retain)float counter;

MainView.m
@synthesize counter;

-(id)init {
if (self = [super init]) {
	[self setCounter:1];
}
return self;
}

subView.h
@class MainView;
@interface subView:UIView {
MainView *mainView
}
@property (nonatomic, retain)MainView *mainView;

subView.m
#import "MainView.h"
@synthesize mainView;
mainView = [[MainView alloc] init];
NSLog(@"%f", [mainView counter]);

 

 

Hope this helps...

Link to comment
Share on other sites

thanks Darth, you code is working fine,

 

but the problem is still there, I just want to create a static var,

 

how can I create a static var inside objc?

 

when subView create, it will +1 counter to the MainView.

 

after I create 5 subView, the MainView counter should return 5.

Link to comment
Share on other sites

static vars work just like they do in C.

 

static unsigned count = 0;

 

One more thing, if you want to have a static var if I my memory serves me well you can not declare it as a property, so you will have to create your own getters and setters and they have to be class methods (declared with + not -). I mean it is not requirement, but you probably don't want to instantiate a class only to read static var...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Try something like this:

 

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"

static unsigned counter = 0;

@implementation MainView

@synthesize timer, subView;

- (id)init 
{
if(self == [super init])
{
	//[self setCounter:1];
	counter++;
}
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;
}


NOTE: This is not the proper use of a "setter", a proper "setter" would be: counter = input;
/*
- (void)setCounter:(float)input {
counter += input;
}*/

- (void)addView {
subView = [[SubView alloc] initWithFrame:[self bounds]];
[self addSubview:subView];
}



NOTE: This is pointless, [super dealloc] will automatically be called.
/*
- (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;
}


NOTE: This is pointless, [super dealloc] will automatically be called.
/*
- (void)dealloc {
[super dealloc];
}*/

@end

 

It seems backwards (or rather circular looking at [Mainview -addView]) that "subViews" are creating "mainViews", but I am not exactly sure what you trying to do here.

Link to comment
Share on other sites

thanks bofors, it's working!

 

I'm write a shooting game, the MainView use counter to record how many SubView had been add,

 

MainView had a NSTimer looping every second for adding SubView if counter less than 10,

 

if user touch the SubView, SubView will remove from MainView and subtract -1 for MainView counter,

 

so the MainView NSTimer can add new SubView.

 

Any suggestion about my code?

Link to comment
Share on other sites

thanks bofors, it's working!

 

I'm write a shooting game, the MainView use counter to record how many SubView had been add,

 

MainView had a NSTimer looping every second for adding SubView if counter less than 10,

 

if user touch the SubView, SubView will remove from MainView and subtract -1 for MainView counter,

 

so the MainView NSTimer can add new SubView.

 

Any suggestion about my code?

 

Do you know that you are creating a new MainView object each time you create a new subView?:

 

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
	self.backgroundColor = [UIColor whiteColor];
	mainView = [[MainView alloc] init];				   // <=======
	NSLog(@"%f",[mainView counter]);
}
return self;
}

 

Again, I do not understand the architecture of your program, but it looks like want you really want to do is to pass a reference to the mainView object to the subViews instead, like this:

 

- (id)initWithFrame:(CGRect)frame mainView:(MainView *)initMainView {
if (self = [super initWithFrame:frame]) {
	self.backgroundColor = [UIColor whiteColor];
	mainView = initMainView;				   // <=======
	NSLog(@"%f",[mainView counter]);
}
return self;
}

 

Are you using Garbage Collection? (I do not know if this is even an option for iPhone)

Link to comment
Share on other sites

Again, I do not understand the architecture of your program, but it looks like want you really want to do is to pass a reference to the mainView object to the subViews instead, like this:

 

- (id)initWithFrame:(CGRect)frame mainView:(MainView *)initMainView {
 if (self = [super initWithFrame:frame]) {
	 self.backgroundColor = [UIColor whiteColor];
	 mainView = initMainView;				   // <=======
	 NSLog(@"%f",[mainView counter]);
 }
 return self;
}

 

Are you using Garbage Collection? (I do not know if this is even an option for iPhone)

 

 

I try to add the script inside MainView, so I can past the mainView to SubView

 

subView = [[SubView alloc] initWithFrame:[self bounds] mainView:self];

 

but there is a error msg say "warning: no -initWithFrame:mainView: method found"

 

if that I mean can't modify the initWithFrame method?

Link to comment
Share on other sites

but there is a error msg say "warning: no -initWithFrame:mainView: method found"

 

if that I mean can't modify the initWithFrame method?

 

 

Yes, you have to declare that method in the SubView Class header file, like this:

 

// SubView.h

#import <UIKit/UIKit.h>

@class MainView;

@interface SubView : UIView {
MainView *mainView;
}

@property (nonatomic, retain)MainView *mainView;


- (id)initWithFrame:(CGRect)frame mainView:(MainView *)initMainView; // <============


@end

Link to comment
Share on other sites

 Share

×
×
  • Create New...