DateSelectView.m 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // DateSelectView.m
  3. // XMEye
  4. //
  5. // Created by XM on 2017/3/20.
  6. // Copyright © 2017年 Megatron. All rights reserved.
  7. //
  8. #import "DateSelectView.h"
  9. #import "Header.h"
  10. @implementation DateSelectView
  11. -(id)initWithFrame:(CGRect)frame{
  12. self = [super initWithFrame:frame];
  13. self.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];
  14. self.tag = 1002;
  15. NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];//设置为英文显示
  16. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  17. NSString *languageStr = [userDefaults objectForKey:@"Language_String"];
  18. if (languageStr == nil || [languageStr isEqualToString:@"auto"]) {
  19. NSArray *languages = [NSLocale preferredLanguages];
  20. NSString *currentLanguage = [languages objectAtIndex:0];
  21. if ([currentLanguage isContainsString:@"zh-Hans"]) {
  22. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  23. }else if ([currentLanguage isContainsString:@"zh-Hant"]) {
  24. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_TW"];
  25. }else if ([currentLanguage isContainsString:@"ko-"]) {
  26. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ko_KR"];
  27. }
  28. }else if ([languageStr isEqualToString:@"english"]){
  29. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  30. }else if ([languageStr isEqualToString:@"zh_cn"]) {
  31. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
  32. }else if ([languageStr isEqualToString:@"zh_tw"]){
  33. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_TW"];
  34. }else if ([languageStr isEqualToString:@"ko_kr"]){
  35. locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ko_KR"];
  36. }
  37. UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
  38. datePicker.center = CGPointMake(ScreenWidth * 0.5, ScreenHeight * 0.5);
  39. datePicker.datePickerMode = UIDatePickerModeDate;
  40. datePicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSince1970:0];
  41. datePicker.maximumDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
  42. datePicker.tag = 1003;
  43. datePicker.locale = locale;
  44. [self addSubview:datePicker];
  45. UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  46. confirmButton.frame = CGRectMake(ScreenWidth * 0.5 - 100, ScreenHeight * 0.5 + 130, 80, 30);
  47. [confirmButton setTitle:TS("OK") forState:UIControlStateNormal];
  48. confirmButton.titleLabel.textAlignment = NSTextAlignmentCenter;
  49. [confirmButton addTarget:self action:@selector(confirmSelectedAction) forControlEvents:UIControlEventTouchUpInside];
  50. [self addSubview:confirmButton];
  51. UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  52. cancelButton.frame = CGRectMake(ScreenWidth * 0.5 + 40, ScreenHeight * 0.5 + 130, 80, 30);
  53. [cancelButton setTitle:TS("Cancel") forState:UIControlStateNormal];
  54. cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
  55. [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
  56. [self addSubview:cancelButton];
  57. self.hidden = YES;
  58. self.date = [NSDate date];
  59. return self;
  60. }
  61. -(void)dateSelectVIewShow{
  62. UIDatePicker *datePicker = (UIDatePicker *)[self viewWithTag:1003];
  63. datePicker.date = self.date;
  64. self.hidden = NO;
  65. }
  66. -(void)confirmSelectedAction{
  67. UIDatePicker *datePicker = (UIDatePicker *)[self viewWithTag:1003];
  68. self.date = datePicker.date;
  69. if ([self.delegate respondsToSelector:@selector(dateSelectedAction:)]) {
  70. [self.delegate dateSelectedAction:YES];
  71. }
  72. self.hidden = YES;
  73. }
  74. -(void)cancelAction{
  75. if ([self.delegate respondsToSelector:@selector(dateSelectedAction:)]) {
  76. [self.delegate dateSelectedAction:YES];
  77. }
  78. self.hidden = YES;
  79. }
  80. @end