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

时间:2014-03-21 18:08:33

标签: ios objective-c

嘿所以我似乎无法弄清楚为什么我的阵列没有从我的其他课程中获取任何数据,这几乎就像它根本没有打电话给其他人一样

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

}

当我尝试添加某些内容时,我的NSLog会打印出来并且没有添加任何内容。

 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>"
 )

先谢谢,你们上次帮我看了一些我不会想到的事情!

1 个答案:

答案 0 :(得分:0)

声明数组:

@implementation ViewController {
    NSMutableArray *mainArray;
}

并在方法中分配(我在ViewDidLoad中执行):

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

注意:20只是一个示例数字。 并确保分配您正在存储的项目。假设你有一个NSObject .m和.h文件。

你会做的事情:

item = [[objectThatYouHave alloc]init];