4

I'm developing an application. In it I need to pass an array from one view controller to another view controller.

How can I do this?

2

4 Answers 4

3

you can do it by defining an array property in second viewcontrollers .h file like:

@interface SecondViewController : UIViewController 
@property(nonatomic, strong)NSArray *array;
@end

Now in FirstViewconrtoller just pass it

SecondViewController *controller = [[SecondViewController alloc]....]
controller.array = yourArray.//the array you want to pass
Sign up to request clarification or add additional context in comments.

2 Comments

in firstview controller i got the data to array like array=[[results valueForKey:@"names"]retain].So i use this array in your way.But in second viewcontroller i got the error like Program received signal: “EXC_BAD_ACCESS” .
try @property(nonatomic, retain)NSArray *array; instead of assign.
1

I wouldn't return directly the reference of the array using return _theArray;. It is usually a bad coding design to do so. A better solution to your problem would be:

In your first controller's .h file:

@interface FirstViewController : UIViewController
{
    NSArray *_theArray;
}

- (NSArray *)theArray;

In your first controller's .m file:

- (NSArray *)theArray
{
    return [NSArray arrayWithArray:_theArray];
}

And wherever you want in your second controller's code:

NSArray *fooArray = [firstControllerReference theArray];

Be aware that in this example, the references of the objects stored in fooArray are the same as the ones stored in theArray. Therefore, if you modify an object in fooArray, it will also be modified in theArray.

Comments

0

just declare them in .h file & assign them property nonatomic & retain then synthesize them. Now create object of class & access that array :) very simple :)

Comments

0

In swift you

The first Viewcontroller in the prepare

var yourArray = [String]()
yourArray.append("String Value")
   let testEventAddGroupsViewController = segue.destination as! 
TestEventAddGroupsViewController
        testEventAddGroupsViewController.testEvent = yourArray

In the second view controller as a global variable

var testEvent = [String]()

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.