ios-王云鹤 把UIdatePicker 嵌入到 UIActionSheet中

  1. 这里简单的解释一下:

    -(void) setUpDatePicker方法用于调用UIDatePicker

    -(void) DatePickerDoneClick:(id) sender方法用于实现隐藏UIdatePicker

    -(void) dateChanged:(id)sender方法实现获取日期结果值的方法。

    如果没有实现效果,别忘记加上协议,这个是比较容易忘记的

  2. -(void) setUpDatePicker
  3. {
  4.     //点击显示时间
  5.     self.actionSheet = [[UIActionSheet alloc] initWithTitle:nil
  6.                                                    delegate:self
  7.                                           cancelButtonTitle:nil
  8.                                      destructiveButtonTitle:nil
  9.                                           otherButtonTitles:nil];
  10.    
  11.     UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"取消"]];
  12.     UISegmentedControl *confirmButton =[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"确定"]];
  13.     [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
  14.     // Add the picker
  15.     self.datePicker = [[UIDatePicker alloc] init];
  16.     self.datePicker.datePickerMode = UIDatePickerModeDate;
  17.     [self.datePicker addTarget:self
  18.                         action:@selector(dateChanged:)
  19.               forControlEvents:UIControlEventValueChanged];
  20.     [self.actionSheet addSubview:self.datePicker];
  21.     [self.actionSheet showInView:self.view];
  22.     [self.actionSheet setBounds:CGRectMake(0,0,320, 500)];
  23.    
  24.     CGRect pickerRect;
  25.     pickerRect = self.datePicker.bounds;
  26.     pickerRect.origin.y = -50;
  27.     self.datePicker.bounds = pickerRect;
  28.     cancelButton.momentary = YES;
  29.     cancelButton.frame = CGRectMake(10.0f, 7.0f, 65.0f, 32.0f);
  30.     cancelButton.segmentedControlStyle = UISegmentedControlStyleBar;
  31.     [cancelButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
  32.     [self.actionSheet addSubview:cancelButton];
  33.  
  34.     cancelButton.tag = 1;
  35.     confirmButton.momentary = YES;
  36.     confirmButton.frame = CGRectMake(245.0f, 7.0f, 65.0f, 32.0f);
  37.     confirmButton.segmentedControlStyle = UISegmentedControlStyleBar;
  38.     [confirmButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
  39.     [self.actionSheet addSubview:confirmButton];
  40.  
  41.     confirmButton.tag = 2;
  42.     [self.actionSheet showInView:self.view];
  43.     [self.actionSheet setBounds:CGRectMake(0,0, 320, 500)];
  44.    
  45. }
  46.  
  47. -(void) DatePickerDoneClick:(id) sender
  48. {
  49.     UIButton *button = (UIButton *)sender;
  50.     if(button.tag == 1)
  51.     {
  52.         [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
  53.     }
  54.    
  55.     if(button.tag == 2)
  56.     {
  57.         [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
  58.     }
  59. }
  60.  
  61. -(void) dateChanged:(id)sender
  62. {
  63.     NSDate *dateValue = [NSDate date];
  64.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  65.     [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  66.     dateValue = ((UIDatePicker *)sender).date;
  67.    
  68.     self.teleplayDate.text = [dateFormatter stringFromDate:dateValue];//[NSString stringWithFormat:@"%@",dateValue];
  69. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值