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. }
posted @ 2013-06-29 22:52  坚固66  阅读(192)  评论(0编辑  收藏  举报