0

我有一个带有日期选择器的视图控制器,并将变量设置为选择的日期。

当我关闭视图控制器时,变量为空。

有什么建议么?

按下“完成”时的 DatePickerView 代码(NSLogs 显示将日期分配给变量):

-(void)done:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];

    AddEditFixturesTableViewController *fixtures = [[AddEditFixturesTableViewController alloc]init];

    fixtures.fixtureDateTime = pickerDateTime;

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *pickerDateTimeString = [formatter stringFromDate:pickerDateTime];
    NSLog(@"Picker Date: %@", pickerDateTimeString);
    NSString *fixturesDateTimeString = [formatter stringFromDate:[fixtures fixtureDateTime]];
    NSLog(@"Fixture Date: %@", fixturesDateTimeString);
}

当我检查是否分配了日期时的代码(NSLogs 显示变量为空):

DatePickViewController *pickerView = [[DatePickViewController alloc]init];

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"dd-MM-yyyy"];
NSString *fixturesDateTimeString = [formatter stringFromDate:fixtureDateTime];
NSLog(@"Fixture Date: %@", fixturesDateTimeString);
NSString *pickerDateTime = [formatter stringFromDate:[pickerView pickerDateTime]];
NSLog(@"Picker Date: %@", pickerDateTime);
4

1 回答 1

0

您的错误在于以下代码:

AddEditFixturesTableViewController *fixtures = [[AddEditFixturesTableViewController alloc]init];
fixtures.fixtureDateTime = pickerDateTime;

在这里,您初始化新的 AddEditFixturesTableViewController 对象并为其设置属性。但是您必须将其完全设置为您现有的对象。所以你必须使用委托。

首先,您必须添加协议。在 DatePickViewController.h 中添加以下内容:

@protocol DatePickViewControllerDelegate <NSObject>

- (void)setNewDate:(NSDate *)date;

@end

@interface DatePickViewController : UIViewController

{
    IBOutlet UIDatePicker *datePicker;
}

@property (nonatomic, weak) id<DatePickViewControllerDelegate> delegate;
@property (nonatomic, strong) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, strong) NSDate *pickerDateTime;

-(IBAction)done:(id)sender;
-(IBAction)dateChanged:(id)sender;

@end

现在修复你的done:方法。

- (void)done:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];

    if (_delegate) {
        [_delegate setNewDate:pickerDateTime];
    }

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *pickerDateTimeString = [formatter stringFromDate:pickerDateTime];
    NSLog(@"Picker Date: %@", pickerDateTimeString);
    NSString *fixturesDateTimeString = [formatter stringFromDate:[fixtures fixtureDateTime]];
    NSLog(@"Fixture Date: %@", fixturesDateTimeString);
}

在您的AddEditFixturesTableViewController界面中添加委托方法。

@interface AddEditFixturesTableViewController : UITableViewController <UITextFieldDelegate, UITextViewDelegate, DatePickViewControllerDelegate>

@property (nonatomic, strong) NSDate *fixtureDateTime;

@property (nonatomic, strong) IBOutlet UILabel *label;
- (IBAction)setDate:(id)sender;
- (void)setNewDate:(NSDate)date;

@end

最后添加委托方法实现:

- (void)setNewDate:(NSDate *)date
{
    fixtureDateTime = date;
}

它会帮助你。祝你好运!

于 2013-07-28T16:29:28.867 回答