实例方法无法读取类实例变量
问题:当从不同的类调用该方法时,类中的变量会以某种方式被忽略。
基本上:我有A类,B类。我在各自的类中也有方法A,方法B。
当从 B 类方法 B 调用方法 A 时,我可以很好地使用 NSLog 值,但是我无法访问 A 类中包含的 NSMutableArray。这是我的问题。
// Class B
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
foodListingObj = [[FoodListing alloc] initWithNibName:@"FoodListing" bundle:nil];
}
return self;
}
- (void)toggleImage //Method B in Class B {
[foodListingObj didToggle:self.indexOfToggledCell];
}
// Method in Class A
- (void)didToggle:(NSIndexPath *)toggledIndexPath{
//[_toggledIndexArray addObject:toggledIndexPath];
[_toggledIndexArray addObject:@"Anything"];
}
// Method in Class A
- (void)checkArray{
// Log the count of the array
// it always says 1 because I intialize the array
// then add an an object like so [_toggledIndexArray addObject@"hi"];
// in my ViewDidLoad Method. Hence it appears that the array is still around
// (not deallocated), but yet my method cannot seem to touch it...
NSLog(@"%i",[_toggledIndexArray count]);
}
// dealloc for Class A
- (void)dealloc{
// I release the array among other things
[_toggledIndexArray release];
}
数组 (_toggledIndexArray) 是在标头中声明的属性,并在 A 类的 viewDidLoad 中初始化,
_toggledIndexArray = [[NSMutableArray alloc] init];
错误是方法 A 似乎不会因我未知的原因影响数组。
Problem: Variables from a class are being overlooked somehow when the method is called from a different class.
Basically: I have Class A, Class B. I also have Method A, Method B each in their respective classes.
When calling Method A from Class B Method B, I can NSLog values fine, however I cannot access an NSMutableArray contained within Class A. There is my issue.
// Class B
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
foodListingObj = [[FoodListing alloc] initWithNibName:@"FoodListing" bundle:nil];
}
return self;
}
- (void)toggleImage //Method B in Class B {
[foodListingObj didToggle:self.indexOfToggledCell];
}
// Method in Class A
- (void)didToggle:(NSIndexPath *)toggledIndexPath{
//[_toggledIndexArray addObject:toggledIndexPath];
[_toggledIndexArray addObject:@"Anything"];
}
// Method in Class A
- (void)checkArray{
// Log the count of the array
// it always says 1 because I intialize the array
// then add an an object like so [_toggledIndexArray addObject@"hi"];
// in my ViewDidLoad Method. Hence it appears that the array is still around
// (not deallocated), but yet my method cannot seem to touch it...
NSLog(@"%i",[_toggledIndexArray count]);
}
// dealloc for Class A
- (void)dealloc{
// I release the array among other things
[_toggledIndexArray release];
}
The array (_toggledIndexArray) is a property declared in the header, and is initialized in Class A's viewDidLoad with
_toggledIndexArray = [[NSMutableArray alloc] init];
The error is the fact that Method A does not seem to affect the array for a reason unknown to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从上面的代码来看,您的问题似乎是每次调用
toggleImage
方法时都会创建 FoodListing 的新实例。这真的是你打算做的吗?如果是,在这种情况下,ViewDidLoad 将不会被调用,因为您只是
分配
和初始化
视图控制器,而不是使用initWithNibName
,因此除非您在自定义 loadView 方法中执行某些操作,否则不会加载视图。但是,我不希望您每次都想创建一个新实例,代码的含义是 FoodListing 对象已经存在并且已经填充了数组。
因此,无论“b 类”是什么,都声明一个 FoodListing 类型的属性。将其设置为您的 FoodListing 视图控制器(这将在创建或呈现类 b 时,或者在首次创建 FoodListing 对象时,您没有提供足够的上下文让我说)。调用属性中保存的对象的方法,而不是创建一个新对象。
From the code above, it looks like your problem is that you are creating a new instance of FoodListing each time your
toggleImage
method is called.Is this really what you intend to do? If it is, ViewDidLoad isn't going to get called in this case, since you are just
alloc
ing andinit
ing the view controller as opposed to usinginitWithNibName
, so unless you are doing something in your custom loadView method, there isn't going to be a view that is loaded.However, I don't expect you do want to create a new instance each time, the implication from your code is that the FoodListing object already exists and has the array populated already.
So, in whatever "class b" is, declare a property of type FoodListing. Set this to your FoodListing view controller (this will be either when class b is created or presented, or when the FoodListing object is first created, you havent given enough context for me to say). Call the methods on the object held in the property instead of creating a new one.
您需要将 B 类中的 FoodListing 对象维护为类变量,而不是总是在toggleImage 函数中分配/初始化它。也许会
You need to maintain the FoodListing object in Class B as class variable and not always alloc/init it in toggleImage function. Perhaps do