Objective c 无法访问另一个类的公共变量

Objective c 无法访问另一个类的公共变量,objective-c,Objective C,我知道这个问题经常被问到,我已经读了很多关于它的书,但我仍然无法让它发挥作用。假设我有两个班,一等和二等。第一个类有一个标签,第二个类希望获取该标签的文本。以下是我所做的: //FirstClass @interface FirstClass : UIViewController { @public UILabel *theLabel; } @property (strong, nonatomic) UILabel *theLabel; @implementation FirstCla

我知道这个问题经常被问到,我已经读了很多关于它的书,但我仍然无法让它发挥作用。假设我有两个班,一等和二等。第一个类有一个标签,第二个类希望获取该标签的文本。以下是我所做的:

//FirstClass
@interface FirstClass : UIViewController
{
@public
    UILabel *theLabel;
}
@property (strong, nonatomic) UILabel *theLabel;

@implementation FirstClass
@synthesize theLabel;


//SecondClass
#import "MainGameDisplay.h"
@interface SecondClass : UIViewController
{
    MainGameDisplay *mainGame;
}
@property (strong, nonatomic) UILabel *theSecondLabel;

@implementation SecondClass

-(void) thisMethodIsCalled {
    mainGame = [[FirstClass alloc] init];

    self.theSecondLabel.text = mainGame.theLabel.text;
    NSLog(@"%@",mainGame.theLabel.text); //Output is '(Null)'
}
label.Text不是nil,因为它每秒都在更改,并且在加载SecondClass视图时在后台运行的另一个控制器上显示标签。如果我完全错了,请有人给我指一下写作方向,或者给我举个例子说明如何做到这一点。多谢各位


编辑:

@实现第一类
@合成标签;
-(无效)viewDidLoad{
[自我监督];
[自启动时钟计数];
}
-(无效)startTickCount{
计时器=[NSTimer scheduledTimerWithTimeInterval:5.0目标:自选择器:@selector(timeChanger)userInfo:nil repeats:YES];
}
-(无效)时间转换器{
theDay++;
NSLog(@“%@”,self.theLabel.text);
若有(第27日)
[自我介绍];
}

差不多就是这样。NSLog按预期输出日期。

方法名称是文本,而不是文本。大小写很重要,使用T值较低的文本会导致错误。

如果这是您的代码,则有两个问题。首先,
文本
不必要地大写。其次,
标签不必要地大写

编辑代码:

-(void) thisMethodIsCalled {
    mainGame = [[MainGameDisplay alloc] init];

    // 'text' shouldn't be capitalized
    // 'theLabel' shouldn't be capitalized
    self.theSecondLabel.text = mainGame.theLabel.text;
    NSLog(@"%@",mainGame.theLabel.text);
}

如果你没有遗漏很多代码

-(void) thisMethodIsCalled {
mainGame = [[MainGameDisplay alloc] init];

self.theSecondLabel.text = mainGame.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text); //Output is '(Null)'
}
不起作用。。没有人可以在alloc init和getting of.text变量之间修改mainGame


[@我只知道这不是答案,但评论的格式很糟糕。我会根据需要编辑或删除它]

我想MainGameDisplay是你的头等舱。然后,为了更新第二类对象中的secondlabel.text,您需要传递一个FirstClass对象,而不是在方法调用中实例化它

我想你需要这样做(这是一个非常简单的例子)

  • 将属性添加到第二个类 @性质(非原子,强)一级*一级 之后:

    1) 创建FirstClass的实例,使其具有名称FirstClass

    2) 创建SecondClass的实例。SecondClass*SecondClass=[[SecondClass alloc]init]

    3) 将第二个类的属性设置为第一个类的实例

    secondClass.firstClass = firstClass;
    
    4) 现在您有了对FirstClass的实际对象的引用,并且可以访问它的属性

    -(void) thisMethodIsCalled {
    
        self.theSecondLabel.text = self.firstClasss.theLabel.text;
        NSLog(@"%@",mainGame.theLabel.text); 
    }
    

    我希望这会有所帮助。

    hd1在我写我的时抢先找到了答案。不,对不起,我的错误,有问题的打字错误。这不是问题所在。现在将进行编辑。这会给我一个错误,无论如何我都会注意到它。即使它被修改,第二个label.text不是等于alloc init的label.text吗?您是否尝试在MainGameDisplay的alloc方法中设置self.theLabel.text?删除的MainGame会显示label.text内容。我真的不知道现在该怎么办。每次调用你的方法时,你都在重置MainGame对象。是的,很抱歉,这是我程序中的实际类。现在我将发布更多的内容。问题是,在您的代码中,您创建了FirstClass的新实例,如果您没有在'init'方法中设置它,那么实际上您没有'theLabel',它是nil。由于向Objective-C中的“nil”发送消息是合法的,并且此操作的结果也是“nil”,因此您将在self.theSecondLabel.text中获得“nil”。但无论如何,多一点实际代码将有助于正确回答。我添加了更多的代码,我认为没有任何其他相关内容需要添加。我从未使用init方法设置过任何东西,它是否与initWithNibName相同,如果是,我会在其中写什么?好的,我明白了。现在,我假设您希望在'thisMethodIsCalled'方法的第二个类中获取此更新。然后必须将第一类对象传递给第二类对象。我将编辑我的答案。是的,SecondClass有另一个计时器,每5秒调用一次“thisMethodIsCalled”,以使标签保持最新。
    -(void) thisMethodIsCalled {
    
        self.theSecondLabel.text = self.firstClasss.theLabel.text;
        NSLog(@"%@",mainGame.theLabel.text); 
    }