变量在dismissViewController上取消

时间:2013-07-28 14:55:24

标签: nsdate viewcontroller

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

当我关闭视图控制器时,变量是无效的。

有什么建议吗?

按下'done'时的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);
}

检查日期是否已分配时的代码(NSLog显示变量为空):

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

1 个答案:

答案 0 :(得分: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;
}

它会帮助你。祝你好运!