Question

I'm working on an iOS app which has 3 text fields and a UIDatePicker.

When I select a time in the DatePicker I'm supposed to display it over the TextField but I can't get it to work.

Here are my files:

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

Can anyone help me figure out how I can solve this problem?

EDIT: also, I'd like to know how can I program an "if" method that checks that the inputted date isn't before today's, i.e., that you've inputted May the 9th and not May the 8th.

Something like "if date < today".

Thank you very much.

Was it helpful?

Solution

try this in viewdidload method

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];

and

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

OTHER TIPS

In your viewDidLoad you have "data", I don't see any initialization of this object. I think displaydate: should update your textfield? There's again "data", I can't see where it comes from, I think it's a global you defined? You shouldn't do that. You use the description, you should not do that, either. From the Docs:

(for description Method)

Discussion The representation is useful for debugging only.

There are a number of options to acquire a formatted string for a date including: date formatters (see NSDateFormatter and Data Formatting Guide), and the NSDate methods descriptionWithLocale:, dateWithCalendarFormat:timeZone:, and descriptionWithCalendarFormat:timeZone:locale:

you should use [self.dateFormatter stringFromDate:] to get a string representation of your date. And if you use properties you get getters and setter, make use of them, it can simplify your development process. For example you could update the textfield in the date property's setter. Feel free to ask for more clarification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top