将iOS DatePicker数据导入textfield

时间:2014-05-09 10:17:45

标签: ios datepicker uitextfield

我正在开发一款iOS应用,它有3个文本字段和一个UIDatePicker。

当我在DatePicker中选择一个时间时,我应该在TextField上显示它,但我无法让它工作。

以下是我的文件:

Login.h

#import <UIKit/UIKit.h>


@interface Login : UIViewController<UIPickerViewDelegate>
{

}

@property (weak) IBOutlet UILabel * label;



@property (weak) IBOutlet UITextField * username;
@property (weak) IBOutlet UITextField * password;
@property (weak) IBOutlet UITextField * birth;
@property (weak) IBOutlet UIButton * entrar;
@property (weak) IBOutlet UIDatePicker * data;

@property (strong, nonatomic) NSDate *dateOfBirth;
@property (strong, nonatomic) NSDateFormatter * dateFormatter;


@property (strong, nonatomic) NSString * etiqueta;
@property (strong, nonatomic) NSString * eti;
@property (strong, nonatomic) NSString * dates;

-(id) initWithBirth: (NSDate *) dateOfBirth;
-(IBAction) displayDate: (id) sender;



@end

Login.m

   #import "Login.h"


@interface Login()
@end

@implementation Login

-(void) viewdDidLoad{

     [super viewDidLoad];
    _eti=@"";
    self.label.text=self.eti;
    _etiqueta = @"Introdueix el teu nom d'usuari, la contrasenya, i la teva data de naixement";
    self.label.text = self.etiqueta;

    self.data = [[UIDatePicker alloc] init];
    [_data setDate:[NSDate date]];
    [self.data addTarget:self action:@selector(updateTextField:)  forControlEvents:UIControlEventValueChanged];
    [self.birth setInputView:_data];

    self.data.datePickerMode = UIDatePickerModeDate;
    self.data.hidden = NO;
    self.data.date = [NSDate date];
    self.dateFormatter = [[NSDateFormatter alloc] init];
    self.dateFormatter.dateStyle = NSDateFormatterLongStyle;
    [self.view addSubview:self.data];


    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

    NSDate * now = [NSDate date];
    [_data setDate:now animated:YES];
    _birth.text = [now description];

    [self.dateFormatter stringFromDate: _dateOfBirth];

}

-(void)datePickerDidChangeDate:(id)sender
{
    NSLog(@"%@",self.data.date);
    self.birth.text=[NSString stringWithFormat:@"%@",self.data.date.description];
}


-(NSString *)formatDate:(NSDate *) date
{
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle: NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"MM'/'dd'/'yyyy"];
    NSString * formattedDate = [dateFormatter stringFromDate: date];
    return formattedDate;

    //NSDate *yesterdaydate=[NSDate dateWithTimeIntervalSinceNow:obj];
}

-(void)dismissKeyboard {
    [_username resignFirstResponder];
    [_password resignFirstResponder];
    [_birth resignFirstResponder];
}


-(id) initWithBirth:(NSDate *)dateOfBirth
{
    if (self = [super init]){
        _dateOfBirth = dateOfBirth;
    }
    return self;

}

-(IBAction) displayDate:(id)sender{
    NSDate * selected = [_data date];
    NSString * dates = [selected description];
    _birth.text = dates;
}


-(IBAction) selector:(id)sender
{
    if (_username.text.length == 0 || _password.text.length == 0 || _birth.text.length == 0)
    {
        [[[UIAlertView alloc] initWithTitle:@"Alerta!"
                                    message:@"Algun camp està buit!"
                                    delegate: nil
                          cancelButtonTitle:@"Cancelar"
                          otherButtonTitles:nil
          ] show];
    }

    else{
        [self performSegueWithIdentifier: @"Menu" sender:self];

    }

}

@end

任何人都可以帮我弄清楚如何解决这个问题吗?

编辑:另外,我想知道如何编写一个“if”方法来检查输入日期是否在今天之前,即你输入的是5月9日而不是5月8日。

类似“如果日期&lt;今天”。

非常感谢。

2 个答案:

答案 0 :(得分:1)

viewDidLoad你有“数据”中,我没有看到此对象的任何初始化。 我认为displaydate:应该更新您的文本字段?还有“数据”,我看不出它来自哪里,我认为这是你定义的全局?你不应该这样做。您使用说明,也不应该这样做。来自Docs:

(用于描述方法)

  

讨论该表示仅对调试有用。

     

有许多选项可以获取日期的格式化字符串   包括:日期格式化程序(请参阅NSDateFormatter和数据格式   指南)和NSDate方法descriptionWithLocale:,   dateWithCalendarFormat:timeZone:,和   descriptionWithCalendarFormat:时区:区域设置:

您应该使用[self.dateFormatter stringFromDate:]来获取日期的字符串表示形式。如果您使用属性获得getter和setter,可以使用它们,它可以简化您的开发过程。例如,您可以更新日期属性的setter中的文本字段。随意请求更多说明。

答案 1 :(得分:1)

在viewdidload方法中尝试这个

self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
            [self.pickerView addTarget:self action:@selector(datePickerDidChangeDate:)  forControlEvents:UIControlEventValueChanged];
            self.pickerView.datePickerMode = UIDatePickerModeDate;
            self.pickerView.hidden = NO;
            self.pickerView.date = [NSDate date];
            self.formatter = [[NSDateFormatter alloc] init];
            self.formatter.dateStyle = NSDateFormatterLongStyle;
            [self.view addSubview:self.pickerView];

-(void)datePickerDidChangeDate:(id)sender
{
    NSLog(@"%@",self.pickerView.date);
  self.textField.text=[NSString stringWithFormat:@"%@",self.pickerView.date.description];    }