Search code examples
iosobjective-cnsmutablearraynsarray

Can't access NSMuatableArray from different class


Looked all the themes about accessing NSMuatableArray from different class and tried all the answers but still can't access that MutableArray from different class.

My code:

ViewController.m

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSArray *printProductImages;
@property (strong, nonatomic) NSArray *printProductNames;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    _printProductImages = [NSArray arrayWithObjects:@"demo_1.jpg", @"demo_2.jpg", @"demo_3.jpg",nil];
    _printProductNames = [NSArray arrayWithObjects:@"text", @"text", nil];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    AnotherViewController *anotherVC = [[AnotherViewController alloc] init];

    switch (indexPath.row) {
        case 0:

           [anotherVC.array1 removeAllObjects]; // does not work and no error
           [anotherVC.array1 addObjectsFromArray:_printProductImages]; // does not work and no error

           [anotherVC.array2 removeAllObjects]; // does not work and no error
           [anotherVC.array2 addObjectsFromArray:_printProductNames]; // does not work and no error

           [self performSegueWithIdentifier:@"AnotherViewController" sender:self];

            NSLog(@"~~Row: 1");
            break;
        case 1:
            NSLog(@"~~Row: 2");
            break;
        default:
            break;
    }

AnotherViewController.h

@interface AnotherViewController: UIViewController

@property (strong, nonatomic) NSMutableArray *array1;
@property (strong, nonatomic) NSMutableArray *array2;

@end

AnotherViewController.m

@implementation AnotherViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _array1 = [NSMutableArray arrayWithObjects:@"demo_3.jpg", @"demo_4", @"demo_1.jpg", @"demo_2.jpg",nil]; // I want to delete these objects and load new objects to this array
    _array2 = [NSMutableArray arrayWithObjects:@"BIG", @"SMALL", @"MEDIUM", @"SUPER GOOD", nil]; // I want to delete these objects and load new objects to this array

Solution

  • The anotherVC you created wasn't added to the view hierarchy, so its view property hasn't been accessed which means viewDidLoad in AnotherViewController hasn't been called before you modify anotherVC.array1, at that point, anotherVC.array1 is nil.

    Try the code below, I manually access its view by anotherVC.view, it may not a good idea but I will show you the mechanism.

    AnotherViewController *anotherVC = [[AnotherViewController alloc] init];
    anotherVC.view; // access its view property so viewDidLoad will be called
    switch (indexPath.row) {
        case 0:
    
           [anotherVC.array1 removeAllObjects];
           [anotherVC.array1 addObjectsFromArray:_printProductImages];
           [anotherVC.array2 removeAllObjects];
           [anotherVC.array2 addObjectsFromArray:_printProductNames];
    
           [self performSegueWithIdentifier:@"SquarePhotos" sender:self];
    
            NSLog(@"~~Row: 1");
            break;
        case 1:
            NSLog(@"~~Row: 2");
            break;
        default:
            break;
    }
    

    BTW you'd better create the NSMutableArray in AnotherViewController's init method.

    - (id) init 
    {
        self = [super init]  ;
        if (self) {
            _array1 = [NSMutableArray arrayWithObjects:@"Some Objects",nil]; 
            _array2 = [NSMutableArray arrayWithObjects:@"Some Objects", nil]; 
        }
        return self; 
    }