MasterViewController.h:
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *listOfBugs;
MasterViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
_listOfBugs = [[NSMutableArray alloc]init];
}
...
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
[_listOfBugs addObject:bug];
}
在上面的代码中,我创建了一个包含具有属性的对象的数组。我想从不同的类访问数组中的对象,并将对象添加到同一个数组中。我知道我可以像这样从同一个类创建一个新数组:
ListOfBugsViewController.m:
#import "ListOfBugsViewController.h"
#import "MasterViewController.h"
MasterViewController *myClass = [[MasterViewController alloc]init];
NSMutableArray *array = [myClass.listOfBugs....
但显然这不是我要找的。我想访问已经在数组中的对象,而不是创建一个新数组。
编辑:为了让事情变得更简单,我有几个类,只有一个数组要添加对象。所有类都应该能够向其中添加对象并读取先前添加的对象。如何从未分配和初始化的类中访问同一个数组?
谢谢