简体   繁体   中英

Date Picker in textfield not working

I have a problem with a DatePicker in a textfield in one Line i get an error and I do nor khnow why.

I get the error in the .m fiele in line "@synthesize resDatum;" the error is:Type of property 'resDatum'(NSData *') does not match type of instance variable 'resDatum'('NSDate *_strong')

And I do not khnow why...

Or is there is smarter solution to implement an DatePicker in an text field for ios6 >?

Here my .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

here my .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

Error is with your resDatum variable datatype. Please use NSDate instead of NSData for @property declaration.

The setter is expecting a NSData now instead of NSDate . please change it to NSDate .

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

Change it to

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

The error is in different type in property resDatum e the variable resDatum:

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

if resDatum is a date (like the name suggests) change the property type in NSDate (not NSData).

If you use @synthesize you don't need to declare private variable with the same name, otherwise if you need a private variable with the same name of the property but with different type you can use

@synthesize resDatum = _resDatum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM