0

I have two view controllers accessing one NSObject with an NSMutableArray. The first VC is a tableview that loads the array's objects, and the second VC is where I add more objects. When I popViewControllerAnimated from my second VC to the first VC, the new object is lost and the first VC only loads the original array, even when I reload the tableView in viewWillAppear.

  1. Why is the new object I'm passing into the NSMutableArray from the second VC lost?
  2. How can I make sure I'm adding objects to the NSMutableArray where it can be accessed from any VC?

I have a Groceries class of type NSObject, which has NSMutableArray *groceryList.

// NSObject class
@interface Groceries : NSObject
@property (nonatomic, strong) NSMutableArray *groceryList;
@end

// First VC
#import "Groceries.h"

@interface TableViewController ()
@property (nonatomic, strong) Groceries *groceries;
@end
@implementation TableViewController
- (void)viewDidLoad {
     [super viewDidLoad];

     _groceries = [[Groceries alloc]init];
}

// Second VC
#import "Groceries.h"

@interface AddItemViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) Groceries *groceries;
@end
@implementation AddItemViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    _groceries = [[Groceries alloc]init];

}
// When I press the return button, I'm popping back to First VC
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {

        [[_groceries groceryList]addObject:_textView.text];
        [textView resignFirstResponder];
        [[self navigationController]popViewControllerAnimated:YES];
        return NO;
    }

    return YES;
}

@end
slider
  • 2,736
  • 4
  • 33
  • 69
  • ok, so what should happen if i quit the app ? and launch again. How should your array behave ? – maddy May 08 '16 at 00:47
  • The new object should still be stored in `groceryList`. – slider May 08 '16 at 00:48
  • how about persist data store like NSUserDefaults/coredata/sqlite ? The most simplest and very small amount of data you can go with NSUserDefaults. – maddy May 08 '16 at 00:52
  • Take help here http://stackoverflow.com/questions/8727508/ios-persistent-storage-strategy – maddy May 08 '16 at 00:58
  • Yes, but that's the second step. I want to make sure the object is passed from my second VC --> `NSObject` properly, so I can display it on my first VC when the nav controller is popped. – slider May 08 '16 at 01:00
  • 1
    use singleton for the NSObject! – Teja Nandamuri May 08 '16 at 01:36

1 Answers1

2

There are 2 ways you can approach this:

  1. Create Groceries as singleton class and use the shared instance to update the array and it will be accessible in both the VC

  2. Use delegation to pass data back to firstVC

    Here is classic example for your situation: http://jameslin.herokuapp.com/blog/2013/10/15/hello-world/

  3. You can use NSNotification update the array object, where VC1 will register for notification and VC2 will post the notification and send the notification object.

Community
  • 1
  • 1
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
  • Singletons seem to be the best method. I was unaware that before, I was only assigning new objects to an **instance** of the array, which was only local. – slider May 09 '16 at 01:54