Xcode Saving UIDatepicker to be displayed in a lab

2019-05-23 21:11发布

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

2条回答
再贱就再见
2楼-- · 2019-05-23 21:54

.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];
      */
}
查看更多
我命由我不由天
3楼-- · 2019-05-23 21:58

Here I shows that UIDatePicker in UITableview cell. I hope it would help.

- (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];
}
查看更多
登录 后发表回答