Xcode Saving UIDatepicker to be displayed in a label

11,948

Solution 1

.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];
      */
}

Solution 2

Here I shows that UIDatePicker in UITableview cell.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {   
    // Make a new view, or do what you want here    
    UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];    
    [self.view addSubview:pv];    
    return NO;    
}

If you are using UITableView, then use following in order to show UIDatePicker instead of keyboard.

if (0 == indexPath.row) {
    cell.rightTextField.placeholder = @”Date”;
    cell.rightTextField.text = [coAccident strDateTime];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 185, 0)];
[datePicker addTarget:self action:@selector(changeDateInLabel :) forControlEvents:UIControlEventValueChanged];
cell.rightTextField.inputView = datePicker;
} 

- (IBAction)changeDateInLabel:(id)sender {
    UIDatePicker * datePicker = (UIDatePicker*)sender;
    CAccidentVO* coAccident = [m_arrVehicles objectAtIndex:1];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;   
    [coAccident setStrDateTime:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]];  
    [df release];  
    [m_tvVehicleDetails reloadData];
}
Share:
11,948
Macfan9000
Author by

Macfan9000

Updated on June 04, 2022

Comments

  • Macfan9000
    Macfan9000 about 2 years

    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 :(

  • Macfan9000
    Macfan9000 over 13 years
    When I go to add that in, I get "datePicker" undeclared, however when I update my .h file to have an NSString *datePicker; , I get " request for member 'date' in something not a structure or union"
  • WrightsCS
    WrightsCS over 13 years
    Oh, yea, sorry, define datePicker in your header, using @property, then in your .m, use @synthesize. Then in your "showActionSheet, remove the "UIDatePicker *".
  • WrightsCS
    WrightsCS over 13 years
    Yes, if you use an XIB, then you need to use @property and IBOutlet in your header / @synthesize in .m and connect everything in Interface Builder.
  • Macfan9000
    Macfan9000 over 13 years
    When I go to connect the datePicker to the label, it won't connect. It acts like the label isn't on the interface, even with a @property and IBOutlet in the header and with @synthesize in the .m . In my .h: IBOutlet UILabel *label1; IBOutlet UIDatePicker *datePicker; both with the appropriate @property(nonatomic, retain)IBOutlet UILabel *label1; @property(nonatomic, retain)IBOutlet UIDatePicker *datePicker; and @synthesized them both in the .m
  • WrightsCS
    WrightsCS over 13 years
    datePicker is the UIDatePicker, so naturally it wont connect to your UILabel. You connect label1 to your Label in IB, then connect datePicker to the UIDatePicker in IB
  • Macfan9000
    Macfan9000 over 13 years
    the label1 won't connect to Label at all in IB, I don't have a specific UIDatePicker for the interface, it's all in code (that I know of so far, I never "dragged" anything from the library to the screen
  • WrightsCS
    WrightsCS over 13 years
    Okay, so if you create your datePicker in code, then theres no need to hook it up in IB, @property & @synthesize are important though.
  • Macfan9000
    Macfan9000 over 13 years
    Something isn't right then because the label doesn't become updated upon selection of date.
  • Macfan9000
    Macfan9000 over 13 years
    here's what the end of the action sheet looks like ... 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]; }
  • WrightsCS
    WrightsCS over 13 years
    Did you connect the label in Interface Builder?
  • Macfan9000
    Macfan9000 over 13 years
    It won't, when I go to connect anything to the label, nothing will happen. it acts like the label isn't even there but it is.
  • WrightsCS
    WrightsCS over 13 years
    I've edited my answer with more code and a screenshot. Beyond this, you can hire me to write your app for you ;-)
  • Macfan9000
    Macfan9000 over 13 years
    NOW I'm getting some activity on the Label updating :) but whenever I choose something it just says (null) :( I put my updated code above. You've been a HUGE HELP! Thx !
  • Macfan9000
    Macfan9000 over 13 years
    I gave props :) but unfortunately I've messed with this code for literally hours and all I've gotten still was a (null) displayed. Thank you though for getting me this far!!! :D