从另一个类获取整数值 [英] Get integer value from another class

查看:102
本文介绍了从另一个类获取整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我以前做过这个,但我不记得怎么做了。



我有一个整数,我希望能够在我的另一个类中进行更改。



如何我是这样做的吗?

  MainViewClass:UIViewController {
int score;
}


#importMainViewClass.h
OtherClass:MainViewClass {

}

然后在.C of OtherClass中我希望能够使用变量得分。



我该怎么做?



我在互联网上搜索过,并试过几件事来试图让它上班不会成功。



感谢您的期待!
祝你有美好的一天。

解决方案

在MainViewClass.h中,您需要将int添加为该类的属性。

  @property(nonatomic,readwrite)int score; 

然后在MainViewClass.m中,您需要使用以下方法合成属性:

  @synthesize得分; 

现在,在您的OtherClass中对MainViewClass进行子类化,您可以使用以下某些组合来访问它的属性。 / p>

在你的OtherClass.h中添加



MainViewClass * mainViewClass;



在您的OtherClass.m中,只要您需要访问乐谱,您就应该能够访问它。

  mainViewClass =(MainViewClass *)self.parent; 

然后使用分数,

  mainViewClass.score; 


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.

解决方案

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;

这篇关于从另一个类获取整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆