Xcode保存UIDatepicker以显示在标签中

时间:2011-01-30 04:45:30

标签: iphone uidatepicker uiactionsheet

以下代码显示我的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];
}

一切正常,但我想做的是将用户选择显示在我已命名为"的标签中。 label1"。有人可以帮忙吗?谢谢!

更新:

以下是更新后的代码:

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

}
 对于.m来说,我的.xib有'标签'正确绑定:)只要我选择我的日期时得到一个(null)值:(

2 个答案:

答案 0 :(得分:2)

·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


同时将以下内容添加到showActionSheet:

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

这应该从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];
      */
}

答案 1 :(得分:1)

我在UIDatePicker单元格中显示UITableview。我希望它会有所帮助。

- (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;    
}

如果您使用的是UITableView,请使用以下内容以显示UIDatePicker而不是键盘。

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