David Biga
David Biga

Reputation: 177

Access NSMutableArray from another class - Objective C

I have a main ViewController that contains a desginated class. Within that ViewController there is a Container that is linked to an embed ViewController. Within that embed ViewController I am creating an NSMutableArray. I am not trying to access that array inside the main ViewController. I know that if I use:

create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];
NSLog(@"%@",myScript.selectedCells);

The NSLog will output null because I am creating a new ViewController and that gets rid of the already set array. So my question is how can I access that array without overwriting it?

UPDATE:

Heres where the NSMutableArray is being created:

create_challenge_peopleSelect.h:

@property (strong, nonatomic) NSMutableArray *selectedCells;

create_challenge_peopleSelect.m:

    if([selectedCells containsObject:label.text])
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [selectedCells removeObjectIdenticalTo:label.text];

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [selectedCells addObject:label.text];
    }

This class is the container class off the main ViewController

No I want to access the selectedCells within my main ViewController, I have been doing things such as:

create_challenge_peopleSelect *myScript = [[create_challenge_peopleSelect alloc] init];

I would prefer to stay away from the App Delegate If possible.

Upvotes: 0

Views: 1623

Answers (5)

andrewbuilder
andrewbuilder

Reputation: 3791

What you've done in the code provided is set a public property for a mutable array...

@property (strong, nonatomic) NSMutableArray *selectedCells;

The NSMutableArray is not "created" by setting that property. At some point in your code you also have to create the NSMutableArray by initialising...

NSMutableArray *selectedCells = [[NSMutableArray alloc] init];

or by using a convenience method such as...

NSMutableArray *selectedCells = [NSMutableArray arrayWithCapacity:(NSUInteger)<initialising capacity>];

or

NSMutableArray *selectedCells = [NSMutableArray arrayWithArray:(NSArray *)<initialising array>];

Initialising an NSMutableArray is often done only once. If it is repeated, the contents are overwritten against the property used to point to the array. As such, a useful location for this is often within the viewDidLoad view controller lifecycle method.

Upvotes: 0

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

Basically, instead of creating a new view controller, you need to maintain a pointer to the original.

I suggest storing an instance of your UIViewController in the AppDelegate in order to retain the particular instance of the view controller you've created by making it a global variable.

ex. In the App Delegate.h

#import "ViewController.h"
@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic) ViewController *viewController;

Then from whatever view controllers' .m's from which you need to read/write to the variable, create a pointer to the application's app delegate, ex:

#import "AppDelegate.h"

@interface WhateverViewController ()

AppDelegate *mainDelegate;

- (void)viewDidLoad {

    mainDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

}

So wherever you first create that view controller in your code (before ever using it), initialize it using this global variable. ex. If you're using xibs:

mainDelegate.viewController = [[UIViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:mainDelegate.viewController animated:YES];

ex. If you're using storyboards:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
mainDelegate.viewController = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerID"];
[self.navigationController pushViewController:mainDelegate.viewController animated:YES];

(This is assuming it's in a place other than the app delegate in which case the pointer to the App Delegate isn't needed.)

Then when accessing the array from another UIViewController use

mainDelegate.viewController.array

Upvotes: 1

Mohit Manhas
Mohit Manhas

Reputation: 3541

The basic idea here is that in your original class, the array is referred to by a pointer. Your original class would allocate it and presumably load it. Other parts of your program can be handed the contents of the property, which is a pointer, assign that to their own pointer holder, and use it as if you had declared it there. Please use the above code;

MyClass *aClass = [[MyClass alloc] initWithMyInitStuff];
NSMutableArray *ThatArray = aClass.MyArray;

NSLog("Count of ThatArray: %d", [That.Array count]);

Upvotes: 0

Chuck
Chuck

Reputation: 237010

You seem to be unclear on the difference between classes and instances. OK, so, say we have two NSArrays:

NSArray *a = [[NSArray alloc] initWithObjects:@"hello", @"I", @"am", @"an", @"array", nil];
NSArray *b = [[NSArray alloc] initWithObjects:@"so", @"am", @"I", nil];

If I do a.count, I'll get 5 as the answer because the array contains five objects. Meanwhile, if I do b.count, I'll get 3, because that array contains three objects. It isn't that creating b "gets rid of the already set count". They are separate objects completely unrelated to each other.

Your view controller class is the same way. When you create a different instance, it doesn't overwrite the old one -- it's just not the same object. In order to use the original view controller object, you need to get a reference to it.

So how do you get a reference to it? Well, the general answer is you design your app so that the two objects know about each other. There are lots of specific ways to accomplish this. A lot of people will say "Just stick a reference in the app delegate." That is one thing you can do, but it's not always the best choice. It can get out of control if you just stick everything in your app delegate. Sometimes it's the right answer, often other things are the right answer. Another approach is to have an object that knows about both of those objects introduce them to each other. But sometimes there is no such object. So it's situational.

Upvotes: 2

Suyog Patil
Suyog Patil

Reputation: 1620

To access the NSMutableArray from one class to another class use following code.

In the first view controller in which u have declared the object of NSMutableArray, declare the property and synthesize for the same as below,

//In FirstViewcontroller.h class,

@property (nonatomic, strong) NSMutableArray *arrData;

//In FirstViewcontroller.m class

@synthesize arrData;

Also FirstViewcontroller object should be global so you can create the object of FirstViewcontroller in app delegate file.

//appdelegate.h

@property (nonatomic, strong) FirstViewcontroller *objFirst;

//appdelegate.m

@synthesize objFirst;

FirstViewcontroller *objFirst=[[FirstViewcontroller alloc]init];

Now in SecondViewcontroller in which you have to access array, create the share object of Appdelegate file

//SecondViewcontroller.m

 AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];

Then use will get the required array as below,

app.objFirst.arrData

This is your required array I hope it will help you.

Upvotes: 0

Related Questions