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

时间:2023-03-09 09:31:25
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. UISegmentedControl*cancelButton =[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"取消"]];
  11. UISegmentedControl*confirmButton =[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"确定"]];
  12. [self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
  13. // Add the picker
  14. self.datePicker =[[UIDatePicker alloc] init];
  15. self.datePicker.datePickerMode =UIDatePickerModeDate;
  16. [self.datePicker addTarget:self
  17. action:@selector(dateChanged:)
  18. forControlEvents:UIControlEventValueChanged];
  19. [self.actionSheet addSubview:self.datePicker];
  20. [self.actionSheet showInView:self.view];
  21. [self.actionSheet setBounds:CGRectMake(0,0,320,500)];
  22. CGRect pickerRect;
  23. pickerRect = self.datePicker.bounds;
  24. pickerRect.origin.y =-50;
  25. self.datePicker.bounds = pickerRect;
  26. cancelButton.momentary = YES;
  27. cancelButton.frame =CGRectMake(10.0f,7.0f, 65.0f, 32.0f);
  28. cancelButton.segmentedControlStyle =UISegmentedControlStyleBar;
  29. [cancelButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
  30. [self.actionSheet addSubview:cancelButton];
  31. cancelButton.tag =1;
  32. confirmButton.momentary = YES;
  33. confirmButton.frame =CGRectMake(245.0f,7.0f, 65.0f, 32.0f);
  34. confirmButton.segmentedControlStyle =UISegmentedControlStyleBar;
  35. [confirmButton addTarget:self action:@selector(DatePickerDoneClick:) forControlEvents:UIControlEventValueChanged];
  36. [self.actionSheet addSubview:confirmButton];
  37. confirmButton.tag =2;
  38. [self.actionSheet showInView:self.view];
  39. [self.actionSheet setBounds:CGRectMake(0,0,320, 500)];
  40. }
  41. -(void)DatePickerDoneClick:(id) sender
  42. {
  43. UIButton*button =(UIButton*)sender;
  44. if(button.tag ==1)
  45. {
  46. [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
  47. }
  48. if(button.tag ==2)
  49. {
  50. [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
  51. }
  52. }
  53. -(void) dateChanged:(id)sender
  54. {
  55. NSDate*dateValue =[NSDate date];
  56. NSDateFormatter*dateFormatter =[[NSDateFormatter alloc] init];
  57. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  58. dateValue =((UIDatePicker*)sender).date;
  59. self.teleplayDate.text =[dateFormatter stringFromDate:dateValue];//[NSString stringWithFormat:@"%@",dateValue];
  60. }