NSMutableArray丢失了添加的数据

时间:2013-03-04 13:29:34

标签: ios objective-c nsmutablearray

出于学习目的,我正在创建一个简单的应用程序,用于计算两个日期之间的周数。 为此,我创建了

  • BITInputViewController:UITableViewController(包含2行,每一行都有一个日期)
  • BITSelectDateViewController:UIViewController(包含用于选择日期的UIDatePicker)

I BITInputViewController我创建了一个NSMutableArray datesArray,其对象为“开始日期”和“结束日期”。

-(id)init
{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        self.title = @"Calculate weeks";

    datesArray = [NSMutableArray arrayWithObjects:@"Start date", @"End date", nil];
}
return self;

}

如果我转到BITSelectDateView控制器,则应将所选日期添加到此Array。 BITSelectDateViewController中的NSLog显示日期被添加到datesArray。但是,如果我返回到BITInputViewController,此条目将丢失,并且只有开始和结束日期在数组中。

那么我错过了什么?

BITInputViewController.h

#import <UIKit/UIKit.h>
#import "BITSelectDateViewController.h"

@interface BITInputViewController : UITableViewController 

@property (nonatomic, copy) NSMutableArray *datesArray;

-(void)passDate:(NSString *)dateString;

@end

BITInputViewController.m

-(void)passDate:(NSString *)dateString {

   [datesArray addObject:dateString];

    NSLog(@"<IVC> passdate:(NSString *)dateString %@",dateString);

    NSLog(@"<IVC> rowcount datesArray %d", [datesArray count]);

    for (NSObject *d in datesArray) {
        NSLog(@"<IVC> Dump of passdate:(NSString *)dateString %@",d);
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    for (NSString *d in datesArray) {
        NSLog(@"<IVC> viewWillAppear: datesArray: %@",d);
    }

    NSLog(@" %p", datesArray);

}

BITSelectedDateViewController.m

- (void)LabelChange:(id)sender{
    NSLog(@"<SDV> LabelChange: method");

    BITInputViewController *vc = [[BITInputViewController alloc]init];


    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;

    dateLabel.text = [NSString stringWithFormat:@"%@",
                      [df stringFromDate:datePicker.date]];

    [vc passDate:dateLabel.text];

    NSLog(@"<SDV> LabelChange: to passDate %@:",dateLabel.text);

     if (vc.datesArray) {

    for (NSString *d in vc.datesArray) {
        NSLog(@"<SDV> Dump of datesrray in labelChange: %@",d);
    }
    }
    else
        NSLog(@"<SDV>!ivc.datesArray");

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[self view] endEditing:YES];

    NSLog(@"<SDV>dateLabel in viewWillDisappear: %@", dateLabel.text);

}

如果查看LOG,您会看到日期已添加到Array中,但是在InputViewController中它会丢失。

2013-03-04 14:06:11.713[28900:c07] <IVC> viewWillAppear: datesArray: Start date
2013-03-04 14:06:11.714[28900:c07] <IVC> viewWillAppear: datesArray: End date
2013-03-04 14:06:11.717[28900:c07] set dates
2013-03-04 14:06:11.719[28900:c07] set dates
2013-03-04 14:06:13.533[28900:c07] <SDV> datelabel viewWillAppear Mar 4, 2013
2013-03-04 14:06:14.895[28900:c07] <SDV> LabelChange: method
2013-03-04 14:06:14.897[28900:c07] <IVC> passdate:(NSString *)dateString Mar 5, 2013
2013-03-04 14:06:14.897[28900:c07] <IVC> aantal rijen in datesArray 3
2013-03-04 14:06:14.898[28900:c07] <IVC> Dump of passdate:(NSString *)dateString Start date
2013-03-04 14:06:14.898[28900:c07] <IVC> Dump of passdate:(NSString *)dateString End date
2013-03-04 14:06:14.899[28900:c07] <IVC> Dump of passdate:(NSString *)dateString Mar 5, 2013
2013-03-04 14:06:14.899[28900:c07] <SDV> LabelChange: to passDate Mar 5, 2013:
2013-03-04 14:06:14.900[28900:c07] <SDV> Dump of datesrray in labelChange: Start date
2013-03-04 14:06:14.900[28900:c07] <SDV> Dump of datesrray in labelChange: End date
2013-03-04 14:06:14.901[28900:c07] <SDV> Dump of datesrray in labelChange: Mar 5, 2013
2013-03-04 14:06:16.489[28900:c07] <SDV>dateLabel in viewWillDisappear: Mar 5, 2013
2013-03-04 14:06:16.489[28900:c07] <IVC> viewWillAppear: datesArray: Start date
2013-03-04 14:06:16.490[28900:c07] <IVC> viewWillAppear: datesArray: End date

2 个答案:

答案 0 :(得分:4)

@property (nonatomic, copy) NSMutableArray *datesArray;

在这里,您可以制作阵列的副本。您的第二个控制器在自己的副本上运行。您需要将数组作为strong / retain传递。

答案 1 :(得分:0)

终于找到了它。 NSMutableArray丢失了它的数据,因为我没有设置属性 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     //导航逻辑可能会在这里。创建并推送另一个视图控制器。

 BITSelectDateViewController  *selectDateViewController = [[BITSelectDateViewController alloc] init];    
 selectDateViewController.vc = self;// Set this property as you prepare the selectDateViewController

 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:selectDateViewController animated:YES];

}