iphoneuidatepickeruiactionsheet

Xcode Saving UIDatepicker to be displayed in a label


The following code is to show my UIDatepicker:

- (IBAction)showActionSheet:(id)sender {
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"Please Select A Date", @"")]
                                  delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"I've Made My Choice", nil];
    [actionSheet showInView:self.view];
    UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [actionSheet addSubview:datePicker];
}

It all works properly, but what I'd like to do is to display the users choice into a label which I have named " label1 ". Can anyone help? Thanks!

UPDATE:

Here is the updated code:

- (IBAction)showActionSheet:(id)sender {
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                              initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"Please Select A Date", @"")]
                              delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"I've Made My Choice", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
[datePicker addTarget:self
               action:@selector(updateLabel:)
     forControlEvents:UIControlEventValueChanged];  

}

- (void)updateLabel:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label1.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];
[df release];


/* the following allows you to choose the date components
 NSCalendar *calendar = [NSCalendar currentCalendar];

 int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[datePicker date]] hour];
 int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[datePicker date]] minute];

 int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[datePicker date]] year];
 int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[datePicker date]] month];
 int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[datePicker date]] day];
 */

}
That's for the .m , my .xib has the 'label' tied properly too :) just getting a (null) value whenever I choose my date :(


Solution

  • .h

    #import <UIKit/UIKit.h>
    @interface YourViewController : UIViewController {
        IBOutlet UILabel *label1;
    }
    @property (nonatomic, retain) UILabel *label1;
    @end
    

    .m

    @implementation YourViewController
    @synthesize label1;
    /*
         All of your code goes here
     */
    @end
    

    Interface Builder

    enter image description here


    Also add the following to your showActionSheet:

    [datePicker addTarget:self
                   action:@selector(updateLabel:)
         forControlEvents:UIControlEventValueChanged];
    

    This should update your label from datePicker:

    -(void)updateLabel:(id)sender
    {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateStyle = NSDateFormatterMediumStyle;
        label1.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];
        [df release];
    
    
        /* the following allows you to choose the date components
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[datePicker date]] hour];
        int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[datePicker date]] minute];
    
        int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[datePicker date]] year];
        int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[datePicker date]] month];
        int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[datePicker date]] day];
          */
    }