0

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.

3
  • I think you need to share more of your code because from your description I don't really understand what you're trying to do. You want to share an array from your view controller to another view controller? You can set it as a property of it. Pass it as a method parameter. Create a custom init method and pass it there... Create a singleton. There are a lot of options. Commented Apr 16, 2014 at 4:23
  • @davecom what I mean is, so you have to initiate the class to get the classes property set variables or what not. What I am confused on is how to access them without them being rewritten and returning null. Commented Apr 16, 2014 at 4:26
  • y u not use in the nsuserdefaults, Commented Apr 16, 2014 at 6:01

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

6 Comments

This is not working for me, I'm doing what you are saying and in my viewcontroller of the array that is made I am putting mainDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; And in my other class where i want to retrieve that array I am putting: mainDelegate.create_challenge = [[create_challenge_peopleSelect alloc] init]; NSLog(@"%@",mainDelegate.create_challenge.selectedCells); Returns null. I set it in my AppDelegate as: @property (strong, nonatomic) create_challenge_peopleSelect *create_challenge;
@DavidBiga No, you're supposed to create the view controller before using it. I guess I didn't explain it properly... Are you using xibs or storyboards?
storyboards, sorry about such late response
OK. To keep the pointer to the view controller, don't use a storyboard segue. I'll edit the above code to show you how to instantiate the view controller in this case.
Just to clarify, you can still use a storyboard, just don't use a UI segue.
|
0

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.

1 Comment

svrushal Can you give me more details on making my FirstViewController global so I can set to the appDelegate?
0

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]);

Comments

0

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.

3 Comments

I didn't vote you down, but the answers section isn't the place to ask for more code. (Referring to your original post of just: More code please.) That sort of comment should be posted in the comments underneath the question.
Besides that, you're misunderstanding his question.
If having less than 50 reputation excused you from posting comments as answers, then the reputation restriction would be meaningless. But thanks for making this a proper answer.

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.