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

标签 iphone ios objective-c datepicker uitextfield

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

我在“@synthesize resDatum;”行的 .m 文件中收到错误错误是:属性类型'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

最佳答案

错误与您的 resDatum 变量数据类型有关。请使用 NSDate 而不是 NSData 进行 @property 声明。

setter 现在需要 NSData 而不是 NSDate。请将其更改为 NSDate

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

改成

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

关于iphone - 文本字段中的日期选择器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17940984/

相关文章:

ios - UIDatePicker 中的最小和最大日期

iPhone Web 服务和表格 View

iphone - 使用mapkit计算两个地址之间的行驶距离?

ios - UIViewController 中的 UISearchBar 崩溃

ios - 我能否以编程方式在 `for` 循环中设置条件?

ios - iPad 应用程序——viewDidLoad 触发前的长黑屏

ios - 如何在 ios 中有效地一次隐藏多个 View

iphone - 通过将行高设置为 0 来隐藏 UITableView?

iphone - 我怎样才能像这样为我的表格单元格创建一个附件 View ?

iphone - 如何在 iOS 中画出思想泡泡