QuizViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // QuizViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 3. 17..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "QuizViewController.h"
  9. #import "CustomButton.h"
  10. #import "CustomTextField.h"
  11. #import "RequestHandler.h"
  12. #import "IDSetViewController.h"
  13. #import "CommonUtil.h"
  14. /**
  15. 해당 메뉴는 멤버만 출력
  16. **/
  17. @interface QuizViewController () <CustomTextFieldDelegate>{
  18. UIBarButtonItem *_btnKeyboardNext, *_btnKeyboardClose; // 키보드 출력시 다음,닫기 버튼추가
  19. UIToolbar *_toolBar; // 툴바 설정
  20. NSMutableArray *_barItems; // 툴바에 추가할 아이템
  21. UIBarButtonItem *_btnFlexibleSpace; // 여백 설정
  22. }
  23. @end
  24. @implementation QuizViewController
  25. @synthesize signUpModel;
  26. // 마스터가 멤버초대시 설정한 퀴즈 가져옴
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self initUI];
  30. [self requsetMasterQuiz];
  31. }
  32. // UI 설정
  33. - (void)initUI {
  34. [self.navigationController.navigationBar setHidden:YES];
  35. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  36. signUpModel.phone = _phoneNum;
  37. _btnNext.enabled = NO;
  38. [_lblStep setColor:kUITextColor03 text:[NSString stringWithFormat:@"%@", @"/ 5"]];
  39. _txtYear.delegate = self;
  40. _txtYear.placeholder = [CommonUtil currentYear];
  41. _txtYear.keyboardType = UIKeyboardTypeNumberPad;
  42. _txtMonth.delegate = self;
  43. _txtMonth.placeholder = [CommonUtil currentMonth];
  44. _txtMonth.keyboardType = UIKeyboardTypeNumberPad;
  45. _txtDay.delegate = self;
  46. _txtDay.placeholder = [CommonUtil currentDay];
  47. _txtDay.keyboardType = UIKeyboardTypeNumberPad;
  48. _txtYear.inputAccessoryView = [self generateAccessoryView];
  49. _txtMonth.inputAccessoryView = [self generateAccessoryView];
  50. }
  51. // 마스터가 설정한 퀴즈의 정답 체크 후 아이디/비밀번호 설정 메뉴로 이동
  52. - (void)requestAnswerQuiz{
  53. NSString *answer = [NSString stringWithFormat:@"%@%@%@",_txtYear.text,_txtMonth.text,_txtDay.text];
  54. //parameters
  55. NSDictionary *parameter = @{@"phone": signUpModel.phone ,
  56. @"answer": answer};
  57. NSString *path = [[JDFacade facade]getUrlWithCustAndGroupIDAndInputMemId:API_POST_QUIZ_SEND inputId:@""];
  58. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  59. IDSetViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"IDSetViewController" storyboardName:@"SignUp"];
  60. vc.signUpModel = signUpModel;
  61. [self.navigationController pushViewController:vc animated:YES];
  62. } failure:^(id errorObject) {
  63. JDErrorModel *error = (JDErrorModel *)errorObject;
  64. [[JDFacade facade] alert:error.errorMessage];
  65. }];
  66. }
  67. // 마스터가 멤버초대시 설정한 퀴즈 가져오기
  68. - (void)requsetMasterQuiz {
  69. //parameters
  70. NSDictionary *parameter = @{@"phone": signUpModel.phone};
  71. NSString *path = [[JDFacade facade]getUrlWithCustAndGroupIDAndInputMemId:API_POST_REQUEST_QUIZ inputId:@""];
  72. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[QuizModel class] completion:^(id responseObject) {
  73. QuizModel *quizModel = (QuizModel *) responseObject;
  74. _lblQuiz.text = quizModel.quiz;
  75. } failure:^(id errorObject) {
  76. JDErrorModel *error = (JDErrorModel *)errorObject;
  77. [[JDFacade facade] alert:error.errorMessage];
  78. }];
  79. }
  80. // 다음 버튼 터치시 퀴즈정답 체크
  81. - (IBAction)btnNextTouched:(id)sender {
  82. [self requestAnswerQuiz];
  83. }
  84. // 메뉴 닫기
  85. - (IBAction)btnCancelTouched:(id)sender {
  86. [self dismissViewControllerAnimated:YES completion:nil];
  87. }
  88. // 년,월,일 입력 될 시 다음버튼 활성화
  89. - (void)textFieldDidEndEditing:(UITextField *)textField{
  90. // 월 입력시 1자리만 입력 된 경우 앞자리에 0추가
  91. if (_txtMonth.text.length == 1) {
  92. _txtMonth.text = [NSString stringWithFormat:@"0%@",_txtMonth.text];
  93. }
  94. // 일 입력시 1자리만 입력 된 경우 앞자리에 0추가
  95. if (_txtDay.text.length == 1) {
  96. _txtDay.text = [NSString stringWithFormat:@"0%@",_txtDay.text];
  97. }
  98. //년 4글자, 월 2글자, 일 2글자 입력시 다음 버튼 활성화
  99. if (_txtYear.text.length >= 4 && _txtMonth.text.length >= 2 && _txtDay.text.length >=2){
  100. _btnNext.enabled = YES;
  101. } else {
  102. _btnNext.enabled = NO;
  103. }
  104. }
  105. // 년,월,일 최대 입력 글자 설정
  106. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  107. if ([textField isEqual:_txtYear]) {
  108. if (textField.text.length >= 4 && range.length == 0) {
  109. return NO;
  110. }
  111. } else if([textField isEqual:_txtMonth]){
  112. if (textField.text.length >= 2 && range.length == 0) {
  113. return NO;
  114. }
  115. } else if ([textField isEqual:_txtDay]){
  116. if (textField.text.length >= 2 && range.length == 0) {
  117. return NO;
  118. }
  119. }
  120. return YES;
  121. }
  122. // 키보드 출력 시 다음,닫기 버튼 추가
  123. - (UIView *)generateAccessoryView {
  124. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  125. _toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 44.0)];
  126. _toolBar.barStyle = UIBarStyleBlack;
  127. _toolBar.translucent = YES;
  128. _barItems = [NSMutableArray new];
  129. _btnFlexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  130. _btnKeyboardNext = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"다음", @"다음")
  131. style:UIBarButtonItemStylePlain
  132. target:self
  133. action:@selector(goNextField)];
  134. _btnKeyboardClose = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"닫기", @"닫기")
  135. style:UIBarButtonItemStylePlain
  136. target:self
  137. action:@selector(hideKeyboard)];
  138. _btnKeyboardClose.tintColor = [UIColor whiteColor];
  139. _btnKeyboardNext.tintColor = [UIColor whiteColor];
  140. [_barItems addObject:_btnKeyboardClose];
  141. [_barItems addObject:_btnFlexibleSpace];
  142. [_barItems addObject:_btnKeyboardNext];
  143. [_toolBar setItems:_barItems animated:YES];
  144. return _toolBar;
  145. }
  146. // 키보드에서 다음버튼 터치시 이동 설정
  147. - (void)goNextField {
  148. if ([_txtYear isEditing]) {
  149. [_txtMonth becomeFirstResponder];
  150. return;
  151. }
  152. if ([_txtMonth isEditing]) {
  153. [_txtDay becomeFirstResponder];
  154. return;
  155. }
  156. }
  157. // 키보드에서 닫기버튼 터치시 키보드 내려감
  158. - (void)hideKeyboard {
  159. [_txtYear resignFirstResponder];
  160. [_txtMonth resignFirstResponder];
  161. [_txtDay resignFirstResponder];
  162. }
  163. - (void)didReceiveMemoryWarning {
  164. [super didReceiveMemoryWarning];
  165. }
  166. @end