Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

objective c - Get integer value from another class

I know i have done this before, but I just cant remember how to do it.

I have a integer, that i want to be able to change in another class of mine.

how do i do it?

MainViewClass : UIViewController {
int score;
}


#import "MainViewClass.h"
OtherClass : MainViewClass{

}

Then in the .m of OtherClass i want to be able to use the variable score.

How do i do this?

I have searched around the internet, and have tried several things to try to get it to work will no success.

Thanks for looking! Have a wonderful day.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your MainViewClass.h, you'll want to add your int as a property of that class.

@property (nonatomic, readwrite) int score;

Then in your MainViewClass.m, you'll want to synthesize the property using:

@synthesize score;

Now since your subclassing MainViewClass in your OtherClass you can access it's properties using some combination of the following.

In your OtherClass.h add

MainViewClass *mainViewClass;

in your OtherClass.m wherever you need access to the score, you should be able to access it as such.

mainViewClass = (MainViewClass *) self.parent;

and then the score using,

mainViewClass.score;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...