0

I am using an NSMutableArry to access ids at another viewController. when I add the object to the array I close the window go to another object. when I add the second object it shows only one object in the array.

ViewCotrlloerA.h

@property (strong, nonatomic)NSMutableArray *bookMarkItems;

ViewControllerA.m

- (IBAction)saveForLater:(UIButton *)sender 
{
    [self.bookMarkItems addObject:story_id];

    NSLog(@"%@", bookMarkItems);
    NSLog(@"%lu", (unsigned long)[bookMarkItems count]);
}

ViewControllerB.h

@class ViewControllerA;

@property (strong, nonatomic)ViewControllerA *sc;

ViewControllerB.m

self.sc.bookMarkItems = [[NSMutableArray alloc] init];
NSString *story_ids = [self.sc.bookMarkItems componentsJoinedByString:@","];

At this point story_ids is nil. Can anyone tell me why? I need to be able to store many story ids in controllerA and view them in ControllerB.

dan
  • 23
  • 7

3 Answers3

2

While there are ways for you to access ViewController A's properties from another view controller, it would be much better for you to give it a strong array property of its own to pass the pointer from the other view controller.

//View controller being passed data
@property (nonatomic, strong) NSMutableArray *passedArray;

And then you need a custom init method that has an array parameter.

-(instancetype) initWithArray: (NSMutableArray *) array
{
    self = [super init];
    if (self) {
        //INIT ALL YOUR OTHER PROPERTIES
        self.passedArray = array;
    }
    return self;

}

This should give you access to the same array instance you were working with in the previous view controller. Remember to use this custom init method when you instantiate this view controller.

Chase
  • 2,206
  • 1
  • 13
  • 17
  • I already have the array property as strong. I am trying to understand how this works. How does the init method is being treated? – dan Mar 02 '15 at 19:35
  • You're passing the array from one view controller to the other. You do this when your second view controller is initialized in your previous view controller. You would call this init method to instantiate your view controller before you push it to the window. – Chase Mar 02 '15 at 19:38
  • how to access the instance of the init method in ViewControllerB? Something is not right I cannot right to the array now, – dan Mar 02 '15 at 19:48
  • No you use that method in ViewController A to instantiate viewController B. It would look like this ViewControllerB *viewB = [[ViewControllerB alloc] initWithArray: self.bookMarkItems] . Then you would push the view controller onto the screen [self presentViewController:viewB animated:YES completion:nil]; -- (note this might be different depending on what kind of viewController it is). – Chase Mar 02 '15 at 19:53
  • This does not work for me :/. I have 3 controllers. first controller need to read all the values in the array at any time. whether it's empty or holds 30 ids. second controller is just the story which in between the two. third controller is the option controller that has the saveForLater action. when that action is clicked I need to add an id to the array. and when I go back to the main page through the second controller I need to be able to read the array value. Does it make sense? – dan Mar 02 '15 at 20:35
  • Since I am using seque, can i do something like this? [segue.destinationViewController initWithArray:passedArry]; when I do it there is an warning. (expression result unused) – dan Mar 02 '15 at 21:06
  • You're passing a pointer to the array around so when you update the array all classes that have been passed the pointer will have the updated value. This is why we set the property to strong. The segue complicates this though. You're going to have to read this thread in order to learn how to pass objects while using a storyboard. http://stackoverflow.com/questions/7864371/ios-how-to-pass-prepareforsegue-an-object – Chase Mar 02 '15 at 21:22
0

The reason there is nothing in the array at that point is because when you call self.sc.bookMarkItems = [[NSMutableArray alloc] init]; you are setting self.sc.bookMarkItems to a new, empty array. You shouldn't have to initialize the array in ViewControllerB if you initialize it in ViewControllerA and are accessing it from an instance of ViewControllerA.

Matt Cooper
  • 2,042
  • 27
  • 43
  • still comes out as nil. I was thinking if thats the case then I will not be able to add objects into it because every time ConrollerA is view the array initialized. The count starts from 0. That means I shouldn't initialize the array at all? – dan Mar 02 '15 at 17:58
  • This is the log I get self.sc (null), self.sc.bookMarkItems (null). so the answer to your question is yes – dan Mar 02 '15 at 18:17
0

Since you are forwarding the data to the next viewController (A to B). You should simply create a custom init method and pass the array accordingly. because the object(of viewcontroller A) is getting destroyed that you are trying to access through self.sc

Instead of writing code to this answer I would say you visit proper tutorial of iOS development. because there soon be a time when you would need to send the data backward (b to A).

  • Can you elaborate more info on that custom init method. What keyword to search in the documentation? – dan Mar 02 '15 at 18:03