实例方法无法读取类实例变量

发布于 2024-12-11 19:38:24 字数 1498 浏览 0 评论 0原文

问题:当从不同的类调用该方法时,类中的变量会以某种方式被忽略。

基本上:我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2024-12-18 19:38:24

从上面的代码来看,您的问题似乎是每次调用 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 allocing and initing the view controller as opposed to using initWithNibName, 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.

篱下浅笙歌 2024-12-18 19:38:24

您需要将 B 类中的 FoodListing 对象维护为类变量,而不是总是在toggleImage 函数中分配/初始化它。也许会

if(self.foodListing==nil) { //alloc/init it}else{ //do your thing here.}

You need to maintain the FoodListing object in Class B as class variable and not always alloc/init it in toggleImage function. Perhaps do

if(self.foodListing==nil) { //alloc/init it}else{ //do your thing here.}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文