Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 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 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...