无法从不同 class 访问 NSMuatableArray

Can't access NSMuatableArray from different class

查看了所有关于从不同 class 访问 NSMutableArray 的主题并尝试了所有答案,但仍然无法从不同 class.

访问 MutableArray

我的代码:

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

另一个ViewController.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

您创建的 anotherVC 未添加到视图层次结构中,因此其 view 属性 尚未被访问,这意味着 viewDidLoadAnotherViewController 在你修改 anotherVC.array1 之前没有被调用过,此时 anotherVC.array1 为 nil。

尝试下面的代码,我通过 anotherVC.view 手动访问它的视图,这可能不是一个好主意,但我会向您展示其机制。

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;
}

顺便说一句,你最好在 AnotherViewController 的 init 方法中创建 NSMutableArray。

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


为 UI

中的 AnotherViewController 设置任何 viewcontrollerID [与图像中的故事板 ID 相同]
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
       AnotherViewController *anotherVC = [storyboard instantiateViewControllerWithIdentifier:@"Viewcontroller ID"];

    anotherVC.array1=[[NSmutablearray alloc]init];

那就试试

switch (indexPath.row) {
        case 0:

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

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

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

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

这一行是你的大问题,

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

这会创建一个 AnotherViewController 的实例,它与您要转到的实例无关——它永远不会出现在屏幕上,并且会在 didSelectRowAtIndexPath: 完成后立即被释放。您应该获得对您正在 segueing 的实例的引用,您可以在 prepareForSegue 中获得该实例(根本不需要实现 didSelectRowAtIndexPath: )。

我不清楚为什么要在 AnotherViewController 中创建 2 个数组,然后在转到该控制器时删除其中的所有对象。这无论如何都行不通,除非您在控制器的 init 方法中创建数组,因为在 prepareForSegue 时,目标视图控制器的视图尚未加载。如果那是你想要做的,你可以这样做(这假设你直接从单元格到 AnotherViewController 进行 segue),

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    if ([segue.identifier isEqualToString:@"AnotherViewController"]) {
        AnotherViewController *anotherVC = segue.destinationViewController;
        switch (indexPath.row) {
            case 0:
               [anotherVC.array1 removeAllObjects];
               [anotherVC.array1 addObjectsFromArray:_printProductImages];
               [anotherVC.array2 removeAllObjects];
               [anotherVC.array2 addObjectsFromArray:_printProductNames];

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

在 AnotherViewController 中,将 2 个数组(需要是可变数组)的创建移动到 initWithCoder: 方法中