查看了从不同类访问NSMuatableArray的所有主题,并尝试了所有答案,但仍然无法从不同的类访问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
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
答案 0 :(得分:2)
您创建的anotherVC
未添加到视图层次结构中,因此其view
属性未被访问,这意味着viewDidLoad
中的AnotherViewController
在修改anotherVC.array1
之前尚未调用,此时anotherVC.array1
为零。
尝试下面的代码,我通过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;
}
答案 1 :(得分:1)
这一行是你的大问题,
AnotherViewController *anotherVC = [[AnotherViewController alloc] init];
这将创建一个AnotherViewController的实例,它与您正在进行的操作无关 - 它永远不会出现在屏幕上,并且会在didSelectRowAtIndexPath:完成后立即释放。你应该得到一个你正在准备的实例的引用,你在prepareForSegue中得到了(根本没有必要实现didSelectRowAtIndexPath:)。
我不清楚,为什么在AnotherViewController中创建2个数组,然后在切换到该控制器时删除其中的所有对象。除非您在控制器的init方法中创建数组,否则无论如何都无法工作,因为在prepareForSegue时,目标视图控制器的视图尚未加载。如果那是你想要做的,你可以这样做(这假设你直接从单元格到另一个ViewController),
-(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:方法
答案 2 :(得分:0)
为UI中的AnotherViewController设置任何viewcontrollerID [与图像中的storyboardID相同]
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;
}