NSMutableArray 没有从我的添加类中获取数据?



嘿,所以我似乎不能弄清楚为什么我的数组没有从我的其他类获得任何数据,它几乎就好像它没有调用其他的。

 #import "AG_ViewController.h"
 #import "AG_Storage.h"
 #import "AG_AddItemViewController.h"
 @interface AG_ViewController ()
 @property NSMutableArray *mainArray;
 @end
 @implementation AG_ViewController
 - (void)viewDidLoad
 {
[super viewDidLoad];
self.mainArray = [[NSMutableArray alloc] init];
[self loadInitialData];
 }
 - (void)loadInitialData
 {
// Do any additional setup after loading the view, typically from a nib.
AG_Storage *item1 = [[AG_Storage alloc] init];
item1.itemName = @"Test1";
[self.mainArray addObject:item1];

AG_Storage *item2 = [[AG_Storage alloc] init];
item2.itemName = @"Test2";
[self.mainArray addObject:item2];
NSLog(@"%@", self.mainArray);
 }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return [self.mainArray count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableViewer cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"thisCell"];
AG_Storage *toDoItem = [self.mainArray objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
    cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
 }
 - (IBAction)unwindToList:(UIStoryboardSegue *)segue
 {
AG_AddItemViewController *source = [segue sourceViewController];
AG_Storage *item = source.store;
NSLog(@"%@", item);
if (item != nil) {
    NSLog(@"%@", source);
    NSLog(@"%@", item);
    [self.mainArray addObject:item];
    NSLog(@"%@", self.mainArray);
    [tableView reloadData];
 }
 }
- (void)didReceiveMemoryWarning
 {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }


 @end

在AG_AddItemViewController我有这个

 - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.doneButton) return;
if (self.textField.text.length > 0) {
    self.store =[[AG_Storage alloc] init];
    self.store.itemName = self.textField.text;
    self.store.completed = NO;
    NSLog(@"%@", self.store);
}
}

当我尝试添加一些东西时,我的NSLogs会打印出这个,并且没有添加任何东西。

 2014-03-21 14:08:24.395 AgendaBk[25633:a0b] (
"<AG_Storage: 0x89af2f0>",
"<AG_Storage: 0x89a5ee0>"
 )
 2014-03-21 14:08:30.405 AgendaBk[25633:a0b] <AG_Storage: 0xc0e3000>
 2014-03-21 14:08:30.409 AgendaBk[25633:a0b] (
"<AG_Storage: 0x897b5c0>",
"<AG_Storage: 0x898db60>"
 )

提前感谢,你们上次帮我看到了我没想到的东西!

声明数组:

@implementation ViewController {
    NSMutableArray *mainArray;
}

并在方法中分配它(我在ViewDidLoad中这样做):

mainArray = [[NSMutableArray alloc] initWithCapacity:20];

注:20只是一个示例数字。确保你分配了你要储存的物品。假设你有一个NSObject。m和。h文件。

你可以这样做:

item = [[objectThatYouHave alloc]init];

最新更新