0

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

  • BITInputViewController : UITableViewController (包含 2 行,每行一个日期)
  • BITSelectDateViewController : UIViewController (包含一个 UIDatePicker 来选择日期)

我 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 控制器,则应将所选日期添加到此数组中。BITSelectDateViewController 中的 NSLogs 显示日期已添加到 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,您会看到日期已添加到数组中,但在 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

.

4

2 回答 2

4
@property (nonatomic, copy) NSMutableArray *datesArray;

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

于 2013-03-04T13:33:46.077 回答
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];

}

于 2013-03-09T15:09:26.713 回答