1

I have two classes. First one downloads data images and adds them to array one after another. When it's done with all 16 of them runs action to switch view to the second one. My problem here is how to access the array from first class in second class?

Here is the code:

first.h

@interface first{
    NSMutableArray *imagesArray;
}
@property(nonatomic,retain)NSMutableArray *imagesArray;

(array is synthetized in .m file)

second.h

#import"second";
#import"first.h"

second.m

-(void) viewdidload {
    first *another = [[another alloc] init];
    label.text =  [NSString stringWithFromat:@"%i",[another.imagesArray count]];
    imageView.image = [another.imagesArray objectAtIndex:0];
}

label shows 0 and imageView shows nothing. Any ideas?

2

2 Answers 2

0

Please get a good understanding about Object-Oriented Features & Object Communication in Objective-C

Do not create a new instance of first class. This will create a new first memory object.

You have to access the existing imagesArray class object that created in existing first class.

To do this, declare a NSMutableArray property in your second.m as weak and assign it from first.m class while you allocating second.m.

Example:

@interface second {

    NSMutableArray *parentImagesArray;
}

@property(nonatomic, weak)NSMutableArray *parentImagesArray;

first.m

You will have the similar code in your first.m file

second *secondView = [[second alloc] init];
secondView.parentImagesArray = self.imagesArray; //assign the weak property.

second.m

label.text =  [NSString stringWithFromat:@"%i",[self.parentImagesArray count]];
imageView.image = parentImagesArray objectAtIndex:0];
Sign up to request clarification or add additional context in comments.

Comments

0

there is two way to achieve your goal...

1) FIRST (pass array from first Controller to Second Controller)

make array object in second controller.h then

from first controller where you present second controller

second.array_object = first.array_name;

In this way you got the array in second controller...

2) declare array as globle

Declare array before import statement.
in first.h file

NSMutableArray *arr1;
#import<UIKit/UIKit.h>

now whenever and wherever you want to use this array import .h file in which you declare this array and use it directly without creating any objects...

hope this helps you...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.