【问题标题】:Date Picker in textfield not working文本字段中的日期选择器不起作用
【发布时间】:2013-07-30 07:37:52
【问题描述】:

我在一行的文本字段中遇到了 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

【问题讨论】:

  • 试试@property (nonatomic, strong) NSData *resDatum; ,不要写retain...让我知道它是否有效!!!??
  • @NiravPatel - strongretain 的同义词。它们在 ARC 下的行为相同。

标签: iphone ios objective-c datepicker uitextfield


【解决方案1】:

错误在于您的 resDatum 变量数据类型。请使用NSDate 而不是NSData 声明@property

setter 现在期待 NSData 而不是 NSDate。请改成NSDate

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

改成

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

【讨论】:

  • 啊,谢谢我整个晚上都看这段代码,我没有看到日期和数据错误-.-
  • 另一个问题:如何使用 Heute 而不是 Today 将 DatePicker 格式更改为欧洲格式(德语)?
【解决方案2】:

错误在于属性 resDatum 和变量 resDatum 中的不同类型:

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

如果 resDatum 是日期(顾名思义),则更改 NSDate 中的属性类型(不是 NSData)。

如果你使用@synthesize,你不需要声明同名的私有变量,否则如果你需要一个与属性同名但类型不同的私有变量,你可以使用

@synthesize resDatum = _resDatum

【讨论】:

  • 次要的 nitpick:在显式合成属性上声明支持变量在 OS X 10.5 中消失了。你不做一个后备变量,你只需使用等号右侧的名称来声明你想要的影子变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多