文本字段中的日期选择器不起作用

时间:2013-07-30 07:37:52

标签: iphone ios objective-c datepicker uitextfield

我在一行的文本字段中遇到DatePicker的问题我得到了一个错误,我也不知道为什么。

我在.m fiele中得到了错误" @synthesize resDatum;"错误是:属性类型' resDatum'(NSData *')与实例变量的类型不匹配' resDatum'(' NSDate * _strong')

我不知道为什么......

或者是否有更智能的解决方案在ios6>的文本字段中实现DatePicker?

这是我的.h:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface Reservieren : UIViewController <MFMailComposeViewControllerDelegate> {

    NSDate *resDatum;
    UIActionSheet *dateSheet;

    IBOutlet UITextField *Anzahl;
    IBOutlet UITextField *Nummer;
    IBOutlet UISegmentedControl *draussendrinnen;
    UITextField *Datum;    

    NSString *draussendrinnenstring;    
}

@property (nonatomic, retain)IBOutlet UITextField *Anzahl;
@property (nonatomic, retain)IBOutlet UITextField *Nummer;
@property (nonatomic, retain) NSData *resDatum;
@property (nonatomic, retain) IBOutlet UITextField *Datum;

- (void)setDate;
- (void)dismissDateSet;
- (void)cancelDateSet;

- (IBAction)Reservieren_zuruck:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
- (IBAction)reservieren:(id)sender;
- (IBAction)draussendrinnenauswahl:(id)sender;

@end

这里是我的.m

#import "Reservieren.h"
#import "ViewController.h"

@interface Reservieren ()

@end

@implementation Reservieren
@synthesize Anzahl, Nummer, Datum;
@synthesize resDatum; //-->Type of property 'resDatum'(NSData *') does not match type of instance variable 'resDatum'('NSDate *_strong')

-(void)setDate {   

    //NSLocale *deLocale = [[NSLocale alloc]initWithLocaleIdentifier:@"de_DE"];
    dateSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [dateSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 44, 0, 0);
    UIDatePicker *dateDayPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
    [dateDayPicker setDatePickerMode:UIDatePickerModeDateAndTime];

    [dateSheet addSubview:dateDayPicker];

    UIToolbar *controlToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];

    [controlToolBar setBarStyle:UIBarStyleBlackTranslucent];
    [controlToolBar sizeToFit];

    UIBarButtonItem *spacer =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    UIBarButtonItem *setButton = [[UIBarButtonItem alloc] initWithTitle:@"Auswählen" style:UIBarButtonItemStyleDone target:self action:@selector(dismissDateSet)];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Abbrechen" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelDateSet)];

    [controlToolBar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil]animated:NO];

    [dateSheet addSubview:controlToolBar];
    [dateSheet showInView:self.view];
    [dateSheet setBounds:CGRectMake(0, 0, 320, 485)];        
}


-(void)cancelDateSet {    
    [dateSheet dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)dismissDateSet {
    NSArray *listOfViews = [dateSheet subviews];
    for (UIView *subView in listOfViews) {
        if([subView isKindOfClass:[UIDatePicker class]]) {
            self.resDatum = [(UIDatePicker *)subView date];
        }
    }    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];   
    [dateFormatter setDateFormat:@"dd.MM.yyyy h.mm"];    
    [Datum setText:[dateFormatter stringFromDate: self.resDatum]];    
    [dateSheet dismissWithClickedButtonIndex:0 animated:YES];    
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [self setDate];
    return NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  { 
   self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload {
    [self setDatum:nil];
    [super viewDidUnload];
}
@end

2 个答案:

答案 0 :(得分:1)

您的resDatum变量数据类型出错。请使用NSDate代替NSData进行@property声明。

设置者现在期待NSData而不是NSDate。请将其更改为NSDate

NSDate *resDatum;
@property (nonatomic, retain) NSData *resDatum;

将其更改为

NSDate *resDatum;
@property (nonatomic, retain) NSDate *resDatum;

答案 1 :(得分:0)

错误属性resDatum中的变量resDatum:

属于不同类型
NSDate *resDatum;
@property (nonatomic, retain) NSData *resDatum;

如果resDatum是一个日期(顾名思义),请在NSDate(而不是NSData)中更改属性类型。

如果使用@synthesize,则不需要声明具有相同名称的私有变量,否则如果您需要具有相同属性名称但具有不同类型的私有变量,则可以使用

@synthesize resDatum = _resDatum