从另一个访问类的变量的值-Objective-C [英] Accessing a value of a class's variable from another - objective-c

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

问题描述

这可能是一个愚蠢的问题,但是我很难解决它.问题是,拥有一个实现计数器的类,并在其末尾调用由另一个类控制的视图.我想做的是从第二个访问第一类的计数器的值.我将计数器定义为属性,并尝试从其他类访问它,但我始终将其值设为0.有人可以帮我吗?

This is probably a silly question but I'm having a hard time trying to solve it. The thing is that a have a class that implements a counter and at the end of it procedures it calls a view controlled by another class. What I'd like to do is access the value of the counter of the first class from the second one. I defined the counter as a property and tried to access it from the other class but I always get its value as 0. Can anyone help me out?

谢谢.

我有什么?

Class1.h

@interface Class1 : CCLayerColor <UIAlertViewDelegate>
{
    int movesCounter;
}
@property int movesCounter;

@end

Class1.m

@implementation Class1
@synthesize movesCounter;

//At this point the counter gets incremented and NSLogging its value correctly

@end

Class2.m

#import Class1.h    

@implementation GameOverLayer


-(id) init
{
    if( (self=[super init]))
    {
       Class1 *theClass1 = [[Class1 alloc]init];
       NSString *message = [NSString stringWithFormat:@"It took you %d moves.",theClass1.movesCounter];
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

       [alert show];
       [alert release];
    }
}

我在做什么错了?

推荐答案

您的问题是您要在Class2初始化方法中创建Class1的 new 实例:

Your problem is that you're creating a new instance of Class1 in your Class2 init method:

Class1 *theClass1 = [[Class1 alloc] init];

此新实例是具有成功递增的int的实例的又一个实例,并且由于它是新的内存片,其moveCounter的值仍为0.您真正想要的是指向特定副本的指针您一直在使用的Class1的实例-不需要或不需要alloc/init.尝试在Class2中创建一个可以保存指向Class1的指针的属性:

This new instance is in addition to the one with the int you've been incrementing successfully, and as it's a fresh slice of memory, its value for movesCounter is still 0. What you really want is a pointer to the specific copy of Class1 that you've been working with -- no alloc / init needed or desired. Try making a property in Class2 that can hold your pointer to Class1:

#import "Class1.h"

@interface Class2 : NSObject {
    Class1 *myPointerToClass1;
}
@property (nonatomic, retain) Class1 *myPointerToClass1;

当然还有Class2的.m中的@synthesize myPointerToClass1.现在,Class2准备好保存指向Class1的指针,您只需要填充它即可.在Class1中,在分配和初始化了Class2实例的代码之后,使用如下代码:

And of course @synthesize myPointerToClass1 in the .m of Class2. Now that Class2 is ready to hold a pointer to Class1, you need simply to populate it. In Class1, after the code that alloc's and init's the Class2 instance, use code like this:

myClass2.myPointerToClass1 = self;

只是为了确保我没有忘记任何事情,我使用上面的代码完成了所有这些工作,一切对我来说都很好.


原始答案从这里开始,但是现在不相关了..... 您提到将计数器定义为属性,但是您是否还在实现(.m)文件中综合了实例变量?

Just to make sure I haven't forgotten anything I did all this using your code above and everything worked just fine for me.


original answer begins here, but is now irrelevant..... You mention having defined the counter as a property, but have you also synthesized the instance variable in your implementation (.m) file?

@synthesize myCounter;

这将为您自动创建setter和getter方法.另外,您是否已经通过viewcontroller中的NSLog语句验证了该值是否正在递增?

This will create the setter and getter methods automatically for you. Also, have you verified that the value is being incremented at all, perhaps with an NSLog statement in the viewcontroller?

NSLog(@"myCounter is currently equal to %d", myCounter); // assuming an int

如果涵盖了这些基础知识,也许您可​​以编辑问题,以包括向我们显示更多有关行为异常类如何首先将指针指向变量的代码.

If these basics are covered, maybe you could edit your question to include the code showing us more about how the misbehaving class is getting this pointer to the variable in the first place.

这篇关于从另一个访问类的变量的值-Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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