ios - 在日期选择器上标记时间

标签 ios iphone datepicker

我正在使用 UIdatepicker。我正在从日期选择器中获取时间并在标签上显示日期。一旦我选择了日期,它就会显示在标签上,并且日期选择器会被隐藏起来。现在我希望当我调用选择器时,它应该在我之前在日期选择器上选择的标签的日期显示日期选择器的选择栏。我正在使用的代码如下所示:-

-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"DONE" otherButtonTitles:Nil, nil];
    pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
    pickdate.hidden = NO;
    //pickdate.date = [NSDate date];
    pickdate.datePickerMode = UIDatePickerModeTime;
    [pickdate addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
    [actionsheet addSubview:pickdate];
    [actionsheet showInView:self.view];
    [actionsheet setBounds:CGRectMake(0,0,320, 500)];
    CGRect pickerRect = pickdate.bounds;
    pickerRect.origin.y = -90;
    pickdate.bounds = pickerRect;
}    

- (IBAction)changeDateInLabel:(id)sender
{
    NSLog(@"I Am Changing the values");
    df = [[NSDateFormatter alloc] init];
    df.timeStyle = NSDateFormatterShortStyle;
}

下面的图片可以更详细地描述你

enter image description here

最佳答案

使用 setDate:animated of UIDatePicker...

- (void)setDate:(NSDate *)date animated:(BOOL)animated

将 label.text 转换为 NSDate:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"hh:mm"]; // depends on 12(hh) or 24(HH) 

NSDate *yourDate = [dateFormatter dateFromString:label.text];

关于ios - 在日期选择器上标记时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20424409/

相关文章:

ios - UITextField 未在函数中注册任何文本长度

ios - 制作屏幕 'Edges' (Cocos2D)

iphone - Facebook:我怎样才能随时将照片贴到墙上

iphone - 设置 UIWindow 的 rootViewController 有什么作用?

objective-c - UIImagePickerController 取消按钮不起作用的问题

ios - UITableViewCell 中的 UIScrollview 并维护 ScrollView 页面

iPhone程序崩溃和编译器显示的堆栈报告完全没有用!

jquery - 是否可以在内联 Bootstrap Datepicker 上使用 setDate?

javascript - Angular 的 JQ Lite 函数(triggerHandler 方法)在 Internet Explorer 11 中不起作用

javascript - jQuery DataTable 中的引导日期选择器在 Laravel 应用程序中不起作用