首页> 疑难解答
无法访问另一个类的公共变量
withpy
2022-01-08
简介我知道这个问题经常被问到,我已经阅读了很多,但我仍然无法让它发挥作用。 假设我有两个类,FirstClass和SecondClass。 FirstClass有一个标签,SecondClass想要获得该标签的文本。 这就是我所做的: theLabel.Text不是nil,因为它每秒都在更改,并且还在加
我知道这个问题经常被问到,我已经阅读了很多,但我仍然无法让它发挥作用。 假设我有两个类,FirstClass和SecondClass。 FirstClass有一个标签,SecondClass想要获得该标签的文本。 这就是我所做的:
//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)'
}
theLabel.Text不是nil,因为它每秒都在更改,并且还在加载SecondClass视图时在后台运行的另一个控制器上显示标签。 如果我完全错了,有人可以指点我的写作方向,或者给我一些关于如何做到这一点的例子。 谢谢。
编辑:
@Implementation FirstClass
@synthesize theLabel;
- (void)viewDidLoad {
[self superview];
[self startTickCount];
}
-(void) startTickCount {
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timeChanger) userInfo:nil repeats:YES];
}
-(void) timeChanger {
theDay++;
NSLog(@"%@",self.theLabel.text);
if (theDay <= 9)
self.theLabel.text = [NSString stringWithFormat: @"0%i", theDay];
else
self.theLabel.text = [NSString stringWithFormat: @"%i", theDay];
if (theDay > 27)
[self monthChanger];
}
这就是它。 NSLog按预期输出当天。
如果你没有遗漏大量的代码,
-(void) thisMethodIsCalled {
mainGame = [[MainGameDisplay alloc] init];
self.theSecondLabel.text = mainGame.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text); //Output is '(Null)'
}
将无法工作..没有人可以在alloc init和.text变量之间修改mainGame ....
[@all我知道这不是答案,但评论的格式很糟糕。 我会根据需要编辑或删除它]
我假设MainGameDisplay是你的FirstClass。 然后,为了更新SecondClass对象中的SecondLabel.text,您需要传递FirstClass的对象,而不是在方法调用中实例化它。
我想你需要做这样的事情(这是一个非常简单的例子)
- 将属性添加到SecondClass @property(非原子,强)FirstClass * firstClass;
之后:
1)创建FirstClass的实例,让它具有名称firstClass。
2)创建SecondClass的实例。 SecondClass * secondClass = [[SecondClass alloc] init];
3)将Second类的属性设置为FirstClass的实例
secondClass.firstClass = firstClass;
4)现在您可以引用FirstClass的实际对象并可以访问其属性。
-(void) thisMethodIsCalled {
self.theSecondLabel.text = self.firstClasss.theLabel.text;
NSLog(@"%@",mainGame.theLabel.text);
}
我希望这将有所帮助。
方法名称是文本,而不是文本。 这种情况很重要,使用较低T的文本会导致错误。
如果这是您的代码,那么您有两个问题。 首先, Text
不必要地大写。 其次, TheLabel
不必要地资本化。
编辑代码:
-(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);
}
相关文章
-
将一个片段交易到另一个片段的问题
我有工作代码,当用户从1个碎片到另一个碎片时,但问题是,当打开New Fragment时,旧的碎片也会显示在下面。所以用户看到了新片段,但当他......
-
防止将重复值从文本框添加到列表框
我有一个文本框,用户在其中扫描条形码,然后选择“添加到列表”按钮,将其添加到列表框中。我正在尝试修改它以防止重复添加,但似乎无法找到方法......
-
我可以将结果映射到Dapper中的Tuple吗?
我试图选择2个整数列的列表将结果映射到元组。就像一个例子:return connection.Query >(“从某个表中选择id1,id2”)。ToList(); ...
-
java中的抽象常量
我想创建一个未在超类 中实现的常量,以强制子类实现它。我发现的最佳解决方案(关于这个主题)是创建一个抽象方法......
-
最小宽度等于子宽度
我有一个页眉,内容和页脚元素。这些元素的包装物占窗口宽度的70%。我正在寻找的是一种为这个包装器设置最小宽度的方法。在我的 ...
-
实现一个热编码
我已经理解了一个热编码与神经网络背后的用途和概念。我的问题是如何实施这一概念。比方说,我有一个神经网络可以接受......