XLFormViewController.m 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. //
  2. // XLFormViewController.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "UIView+XLFormAdditions.h"
  26. #import "NSObject+XLFormAdditions.h"
  27. #import "XLFormViewController.h"
  28. #import "UIView+XLFormAdditions.h"
  29. #import "XLForm.h"
  30. #import "NSString+XLFormAdditions.h"
  31. @interface XLFormRowDescriptor(_XLFormViewController)
  32. @property (readonly) NSArray * observers;
  33. -(BOOL)evaluateIsDisabled;
  34. -(BOOL)evaluateIsHidden;
  35. @end
  36. @interface XLFormSectionDescriptor(_XLFormViewController)
  37. -(BOOL)evaluateIsHidden;
  38. @end
  39. @interface XLFormDescriptor (_XLFormViewController)
  40. @property NSMutableDictionary* rowObservers;
  41. @end
  42. @interface XLFormViewController()
  43. {
  44. NSNumber *_oldBottomTableContentInset;
  45. CGRect _keyboardFrame;
  46. }
  47. @property UITableViewStyle tableViewStyle;
  48. @property (nonatomic) XLFormRowNavigationAccessoryView * navigationAccessoryView;
  49. @end
  50. @implementation XLFormViewController
  51. @synthesize form = _form;
  52. #pragma mark - Initialization
  53. -(instancetype)initWithForm:(XLFormDescriptor *)form
  54. {
  55. return [self initWithForm:form style:UITableViewStyleGrouped];
  56. }
  57. -(instancetype)initWithForm:(XLFormDescriptor *)form style:(UITableViewStyle)style
  58. {
  59. self = [self initWithNibName:nil bundle:nil];
  60. if (self){
  61. _tableViewStyle = style;
  62. _form = form;
  63. }
  64. return self;
  65. }
  66. -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  67. {
  68. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  69. if (self){
  70. _form = nil;
  71. _tableViewStyle = UITableViewStyleGrouped;
  72. }
  73. return self;
  74. }
  75. -(instancetype)initWithCoder:(NSCoder *)aDecoder
  76. {
  77. self = [super initWithCoder:aDecoder];
  78. if (self) {
  79. _form = nil;
  80. _tableViewStyle = UITableViewStyleGrouped;
  81. }
  82. return self;
  83. }
  84. - (void)dealloc
  85. {
  86. self.tableView.delegate = nil;
  87. self.tableView.dataSource = nil;
  88. }
  89. - (void)viewDidLoad
  90. {
  91. [super viewDidLoad];
  92. if (!self.tableView){
  93. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds
  94. style:self.tableViewStyle];
  95. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  96. if([self.tableView respondsToSelector:@selector(cellLayoutMarginsFollowReadableWidth)]){
  97. self.tableView.cellLayoutMarginsFollowReadableWidth = NO;
  98. }
  99. }
  100. if (!self.tableView.superview){
  101. [self.view addSubview:self.tableView];
  102. }
  103. if (!self.tableView.delegate){
  104. self.tableView.delegate = self;
  105. }
  106. if (!self.tableView.dataSource){
  107. self.tableView.dataSource = self;
  108. }
  109. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
  110. self.tableView.rowHeight = UITableViewAutomaticDimension;
  111. self.tableView.estimatedRowHeight = 44.0;
  112. }
  113. if (self.form.title){
  114. self.title = self.form.title;
  115. }
  116. [self.tableView setEditing:YES animated:NO];
  117. self.tableView.allowsSelectionDuringEditing = YES;
  118. self.form.delegate = self;
  119. _oldBottomTableContentInset = nil;
  120. }
  121. -(void)viewWillAppear:(BOOL)animated
  122. {
  123. [super viewWillAppear:animated];
  124. NSIndexPath *selected = [self.tableView indexPathForSelectedRow];
  125. if (selected){
  126. // Trigger a cell refresh
  127. XLFormRowDescriptor * rowDescriptor = [self.form formRowAtIndex:selected];
  128. [self updateFormRow:rowDescriptor];
  129. [self.tableView selectRowAtIndexPath:selected animated:NO scrollPosition:UITableViewScrollPositionNone];
  130. [self.tableView deselectRowAtIndexPath:selected animated:YES];
  131. }
  132. [[NSNotificationCenter defaultCenter] addObserver:self
  133. selector:@selector(contentSizeCategoryChanged:)
  134. name:UIContentSizeCategoryDidChangeNotification
  135. object:nil];
  136. [[NSNotificationCenter defaultCenter] addObserver:self
  137. selector:@selector(keyboardWillShow:)
  138. name:UIKeyboardWillShowNotification
  139. object:nil];
  140. [[NSNotificationCenter defaultCenter] addObserver:self
  141. selector:@selector(keyboardWillHide:)
  142. name:UIKeyboardWillHideNotification
  143. object:nil];
  144. }
  145. -(void)viewDidDisappear:(BOOL)animated
  146. {
  147. [super viewDidDisappear:animated];
  148. [[NSNotificationCenter defaultCenter] removeObserver:self
  149. name:UIContentSizeCategoryDidChangeNotification
  150. object:nil];
  151. [[NSNotificationCenter defaultCenter] removeObserver:self
  152. name:UIKeyboardWillShowNotification
  153. object:nil];
  154. [[NSNotificationCenter defaultCenter] removeObserver:self
  155. name:UIKeyboardWillHideNotification
  156. object:nil];
  157. }
  158. - (void)viewDidAppear:(BOOL)animated
  159. {
  160. [super viewDidAppear:animated];
  161. if (self.form.assignFirstResponderOnShow) {
  162. self.form.assignFirstResponderOnShow = NO;
  163. [self.form setFirstResponder:self];
  164. }
  165. }
  166. - (void)didReceiveMemoryWarning
  167. {
  168. [super didReceiveMemoryWarning];
  169. }
  170. #pragma mark - CellClasses
  171. +(NSMutableDictionary *)cellClassesForRowDescriptorTypes
  172. {
  173. static NSMutableDictionary * _cellClassesForRowDescriptorTypes;
  174. static dispatch_once_t onceToken;
  175. dispatch_once(&onceToken, ^{
  176. _cellClassesForRowDescriptorTypes = [@{XLFormRowDescriptorTypeText:[XLFormTextFieldCell class],
  177. XLFormRowDescriptorTypeName: [XLFormTextFieldCell class],
  178. XLFormRowDescriptorTypePhone:[XLFormTextFieldCell class],
  179. XLFormRowDescriptorTypeURL:[XLFormTextFieldCell class],
  180. XLFormRowDescriptorTypeEmail: [XLFormTextFieldCell class],
  181. XLFormRowDescriptorTypeTwitter: [XLFormTextFieldCell class],
  182. XLFormRowDescriptorTypeAccount: [XLFormTextFieldCell class],
  183. XLFormRowDescriptorTypePassword: [XLFormTextFieldCell class],
  184. XLFormRowDescriptorTypeNumber: [XLFormTextFieldCell class],
  185. XLFormRowDescriptorTypeInteger: [XLFormTextFieldCell class],
  186. XLFormRowDescriptorTypeDecimal: [XLFormTextFieldCell class],
  187. XLFormRowDescriptorTypeZipCode: [XLFormTextFieldCell class],
  188. XLFormRowDescriptorTypeSelectorPush: [XLFormSelectorCell class],
  189. XLFormRowDescriptorTypeSelectorPopover: [XLFormSelectorCell class],
  190. XLFormRowDescriptorTypeSelectorActionSheet: [XLFormSelectorCell class],
  191. XLFormRowDescriptorTypeSelectorAlertView: [XLFormSelectorCell class],
  192. XLFormRowDescriptorTypeSelectorPickerView: [XLFormSelectorCell class],
  193. XLFormRowDescriptorTypeSelectorPickerViewInline: [XLFormInlineSelectorCell class],
  194. XLFormRowDescriptorTypeSelectorSegmentedControl: [XLFormSegmentedCell class],
  195. XLFormRowDescriptorTypeMultipleSelector: [XLFormSelectorCell class],
  196. XLFormRowDescriptorTypeMultipleSelectorPopover: [XLFormSelectorCell class],
  197. XLFormRowDescriptorTypeImage: [XLFormImageCell class],
  198. XLFormRowDescriptorTypeTextView: [XLFormTextViewCell class],
  199. XLFormRowDescriptorTypeButton: [XLFormButtonCell class],
  200. XLFormRowDescriptorTypeInfo: [XLFormSelectorCell class],
  201. XLFormRowDescriptorTypeBooleanSwitch : [XLFormSwitchCell class],
  202. XLFormRowDescriptorTypeBooleanCheck : [XLFormCheckCell class],
  203. XLFormRowDescriptorTypeDate: [XLFormDateCell class],
  204. XLFormRowDescriptorTypeTime: [XLFormDateCell class],
  205. XLFormRowDescriptorTypeDateTime : [XLFormDateCell class],
  206. XLFormRowDescriptorTypeCountDownTimer : [XLFormDateCell class],
  207. XLFormRowDescriptorTypeDateInline: [XLFormDateCell class],
  208. XLFormRowDescriptorTypeTimeInline: [XLFormDateCell class],
  209. XLFormRowDescriptorTypeDateTimeInline: [XLFormDateCell class],
  210. XLFormRowDescriptorTypeCountDownTimerInline : [XLFormDateCell class],
  211. XLFormRowDescriptorTypeDatePicker : [XLFormDatePickerCell class],
  212. XLFormRowDescriptorTypePicker : [XLFormPickerCell class],
  213. XLFormRowDescriptorTypeSlider : [XLFormSliderCell class],
  214. XLFormRowDescriptorTypeSelectorLeftRight : [XLFormLeftRightSelectorCell class],
  215. XLFormRowDescriptorTypeStepCounter: [XLFormStepCounterCell class]
  216. } mutableCopy];
  217. });
  218. return _cellClassesForRowDescriptorTypes;
  219. }
  220. #pragma mark - inlineRowDescriptorTypes
  221. +(NSMutableDictionary *)inlineRowDescriptorTypesForRowDescriptorTypes
  222. {
  223. static NSMutableDictionary * _inlineRowDescriptorTypesForRowDescriptorTypes;
  224. static dispatch_once_t onceToken;
  225. dispatch_once(&onceToken, ^{
  226. _inlineRowDescriptorTypesForRowDescriptorTypes = [
  227. @{XLFormRowDescriptorTypeSelectorPickerViewInline: XLFormRowDescriptorTypePicker,
  228. XLFormRowDescriptorTypeDateInline: XLFormRowDescriptorTypeDatePicker,
  229. XLFormRowDescriptorTypeDateTimeInline: XLFormRowDescriptorTypeDatePicker,
  230. XLFormRowDescriptorTypeTimeInline: XLFormRowDescriptorTypeDatePicker,
  231. XLFormRowDescriptorTypeCountDownTimerInline: XLFormRowDescriptorTypeDatePicker
  232. } mutableCopy];
  233. });
  234. return _inlineRowDescriptorTypesForRowDescriptorTypes;
  235. }
  236. #pragma mark - XLFormDescriptorDelegate
  237. -(void)formRowHasBeenAdded:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
  238. {
  239. [self.tableView beginUpdates];
  240. [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:[self insertRowAnimationForRow:formRow]];
  241. [self.tableView endUpdates];
  242. }
  243. -(void)formRowHasBeenRemoved:(XLFormRowDescriptor *)formRow atIndexPath:(NSIndexPath *)indexPath
  244. {
  245. [self.tableView beginUpdates];
  246. [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:[self deleteRowAnimationForRow:formRow]];
  247. [self.tableView endUpdates];
  248. }
  249. -(void)formSectionHasBeenRemoved:(XLFormSectionDescriptor *)formSection atIndex:(NSUInteger)index
  250. {
  251. [self.tableView beginUpdates];
  252. [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:[self deleteRowAnimationForSection:formSection]];
  253. [self.tableView endUpdates];
  254. }
  255. -(void)formSectionHasBeenAdded:(XLFormSectionDescriptor *)formSection atIndex:(NSUInteger)index
  256. {
  257. [self.tableView beginUpdates];
  258. [self.tableView insertSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:[self insertRowAnimationForSection:formSection]];
  259. [self.tableView endUpdates];
  260. }
  261. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue
  262. {
  263. [self updateAfterDependentRowChanged:formRow];
  264. }
  265. -(void)formRowDescriptorPredicateHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue predicateType:(XLPredicateType)predicateType
  266. {
  267. if (oldValue != newValue) {
  268. [self updateAfterDependentRowChanged:formRow];
  269. }
  270. }
  271. -(void)updateAfterDependentRowChanged:(XLFormRowDescriptor *)formRow
  272. {
  273. NSMutableArray* revaluateHidden = self.form.rowObservers[[formRow.tag formKeyForPredicateType:XLPredicateTypeHidden]];
  274. NSMutableArray* revaluateDisabled = self.form.rowObservers[[formRow.tag formKeyForPredicateType:XLPredicateTypeDisabled]];
  275. for (id object in revaluateDisabled) {
  276. if ([object isKindOfClass:[NSString class]]) {
  277. XLFormRowDescriptor* row = [self.form formRowWithTag:object];
  278. if (row){
  279. [row evaluateIsDisabled];
  280. [self updateFormRow:row];
  281. }
  282. }
  283. }
  284. for (id object in revaluateHidden) {
  285. if ([object isKindOfClass:[NSString class]]) {
  286. XLFormRowDescriptor* row = [self.form formRowWithTag:object];
  287. if (row){
  288. [row evaluateIsHidden];
  289. }
  290. }
  291. else if ([object isKindOfClass:[XLFormSectionDescriptor class]]) {
  292. XLFormSectionDescriptor* section = (XLFormSectionDescriptor*) object;
  293. [section evaluateIsHidden];
  294. }
  295. }
  296. }
  297. #pragma mark - XLFormViewControllerDelegate
  298. -(NSDictionary *)formValues
  299. {
  300. return [self.form formValues];
  301. }
  302. -(NSDictionary *)httpParameters
  303. {
  304. return [self.form httpParameters:self];
  305. }
  306. -(void)didSelectFormRow:(XLFormRowDescriptor *)formRow
  307. {
  308. if ([[formRow cellForFormController:self] respondsToSelector:@selector(formDescriptorCellDidSelectedWithFormController:)]){
  309. [[formRow cellForFormController:self] formDescriptorCellDidSelectedWithFormController:self];
  310. }
  311. }
  312. -(UITableViewRowAnimation)insertRowAnimationForRow:(XLFormRowDescriptor *)formRow
  313. {
  314. if (formRow.sectionDescriptor.sectionOptions & XLFormSectionOptionCanInsert){
  315. if (formRow.sectionDescriptor.sectionInsertMode == XLFormSectionInsertModeButton){
  316. return UITableViewRowAnimationAutomatic;
  317. }
  318. else if (formRow.sectionDescriptor.sectionInsertMode == XLFormSectionInsertModeLastRow){
  319. return YES;
  320. }
  321. }
  322. return UITableViewRowAnimationFade;
  323. }
  324. -(UITableViewRowAnimation)deleteRowAnimationForRow:(XLFormRowDescriptor *)formRow
  325. {
  326. return UITableViewRowAnimationFade;
  327. }
  328. -(UITableViewRowAnimation)insertRowAnimationForSection:(XLFormSectionDescriptor *)formSection
  329. {
  330. return UITableViewRowAnimationAutomatic;
  331. }
  332. -(UITableViewRowAnimation)deleteRowAnimationForSection:(XLFormSectionDescriptor *)formSection
  333. {
  334. return UITableViewRowAnimationAutomatic;
  335. }
  336. -(UIView *)inputAccessoryViewForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  337. {
  338. if ((self.form.rowNavigationOptions & XLFormRowNavigationOptionEnabled) != XLFormRowNavigationOptionEnabled){
  339. return nil;
  340. }
  341. if ([[[[self class] inlineRowDescriptorTypesForRowDescriptorTypes] allKeys] containsObject:rowDescriptor.rowType]) {
  342. return nil;
  343. }
  344. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[rowDescriptor cellForFormController:self];
  345. if (![cell formDescriptorCellCanBecomeFirstResponder]){
  346. return nil;
  347. }
  348. XLFormRowDescriptor * previousRow = [self nextRowDescriptorForRow:rowDescriptor
  349. withDirection:XLFormRowNavigationDirectionPrevious];
  350. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:rowDescriptor
  351. withDirection:XLFormRowNavigationDirectionNext];
  352. [self.navigationAccessoryView.previousButton setEnabled:(previousRow != nil)];
  353. [self.navigationAccessoryView.nextButton setEnabled:(nextRow != nil)];
  354. return self.navigationAccessoryView;
  355. }
  356. -(void)beginEditing:(XLFormRowDescriptor *)rowDescriptor
  357. {
  358. [[rowDescriptor cellForFormController:self] highlight];
  359. }
  360. -(void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  361. {
  362. [[rowDescriptor cellForFormController:self] unhighlight];
  363. }
  364. -(XLFormRowDescriptor *)formRowFormMultivaluedFormSection:(XLFormSectionDescriptor *)formSection
  365. {
  366. if (formSection.multivaluedRowTemplate){
  367. return [formSection.multivaluedRowTemplate copy];
  368. }
  369. XLFormRowDescriptor * formRowDescriptor = [[formSection.formRows objectAtIndex:0] copy];
  370. formRowDescriptor.tag = nil;
  371. return formRowDescriptor;
  372. }
  373. -(void)multivaluedInsertButtonTapped:(XLFormRowDescriptor *)formRow
  374. {
  375. [self deselectFormRow:formRow];
  376. XLFormSectionDescriptor * multivaluedFormSection = formRow.sectionDescriptor;
  377. XLFormRowDescriptor * formRowDescriptor = [self formRowFormMultivaluedFormSection:multivaluedFormSection];
  378. [multivaluedFormSection addFormRow:formRowDescriptor];
  379. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  380. self.tableView.editing = !self.tableView.editing;
  381. self.tableView.editing = !self.tableView.editing;
  382. });
  383. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[formRowDescriptor cellForFormController:self];
  384. if ([cell formDescriptorCellCanBecomeFirstResponder]){
  385. [cell formDescriptorCellBecomeFirstResponder];
  386. }
  387. }
  388. -(void)ensureRowIsVisible:(XLFormRowDescriptor *)inlineRowDescriptor
  389. {
  390. XLFormBaseCell * inlineCell = [inlineRowDescriptor cellForFormController:self];
  391. NSIndexPath * indexOfOutOfWindowCell = [self.form indexPathOfFormRow:inlineRowDescriptor];
  392. if(!inlineCell.window || (self.tableView.contentOffset.y + self.tableView.frame.size.height <= inlineCell.frame.origin.y + inlineCell.frame.size.height)){
  393. [self.tableView scrollToRowAtIndexPath:indexOfOutOfWindowCell atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  394. }
  395. }
  396. #pragma mark - Methods
  397. -(NSArray *)formValidationErrors
  398. {
  399. return [self.form localValidationErrors:self];
  400. }
  401. -(void)showFormValidationError:(NSError *)error
  402. {
  403. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 80000
  404. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"XLFormViewController_ValidationErrorTitle", nil)
  405. message:error.localizedDescription
  406. delegate:self
  407. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  408. otherButtonTitles:nil];
  409. [alertView show];
  410. #else
  411. if ([UIAlertController class]){
  412. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"XLFormViewController_ValidationErrorTitle", nil)
  413. message:error.localizedDescription
  414. preferredStyle:UIAlertControllerStyleAlert];
  415. [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
  416. style:UIAlertActionStyleDefault
  417. handler:nil]];
  418. [self presentViewController:alertController animated:YES completion:nil];
  419. }
  420. #ifndef XL_APP_EXTENSIONS
  421. else{
  422. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"XLFormViewController_ValidationErrorTitle", nil)
  423. message:error.localizedDescription
  424. delegate:self
  425. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  426. otherButtonTitles:nil];
  427. [alertView show];
  428. }
  429. #endif
  430. #endif
  431. }
  432. -(void)showFormValidationError:(NSError *)error withTitle:(NSString*)title
  433. {
  434. #if __IPHONE_OS_VERSION_MAX_ALLOWED < 80000
  435. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(title, nil)
  436. message:error.localizedDescription
  437. delegate:self
  438. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  439. otherButtonTitles:nil];
  440. [alertView show];
  441. #else
  442. if ([UIAlertController class]){
  443. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(title, nil)
  444. message:error.localizedDescription
  445. preferredStyle:UIAlertControllerStyleAlert];
  446. [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
  447. style:UIAlertActionStyleDefault
  448. handler:nil]];
  449. [self presentViewController:alertController animated:YES completion:nil];
  450. }
  451. #ifndef XL_APP_EXTENSIONS
  452. else{
  453. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(title, nil)
  454. message:error.localizedDescription
  455. delegate:self
  456. cancelButtonTitle:NSLocalizedString(@"OK", nil)
  457. otherButtonTitles:nil];
  458. [alertView show];
  459. }
  460. #endif
  461. #endif
  462. }
  463. -(void)performFormSelector:(SEL)selector withObject:(id)sender
  464. {
  465. UIResponder * responder = [self targetForAction:selector withSender:sender];;
  466. if (responder) {
  467. #pragma GCC diagnostic push
  468. #pragma GCC diagnostic ignored "-Warc-performSelector-leaks"
  469. [responder performSelector:selector withObject:sender];
  470. #pragma GCC diagnostic pop
  471. }
  472. }
  473. #pragma mark - Private
  474. - (void)contentSizeCategoryChanged:(NSNotification *)notification
  475. {
  476. [self.tableView reloadData];
  477. }
  478. - (void)keyboardWillShow:(NSNotification *)notification
  479. {
  480. UIView * firstResponderView = [self.tableView findFirstResponder];
  481. UITableViewCell<XLFormDescriptorCell> * cell = [firstResponderView formDescriptorCell];
  482. if (cell){
  483. NSDictionary *keyboardInfo = [notification userInfo];
  484. _keyboardFrame = [self.tableView.window convertRect:[keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:self.tableView.superview];
  485. CGFloat newBottomInset = self.tableView.frame.origin.y + self.tableView.frame.size.height - _keyboardFrame.origin.y;
  486. UIEdgeInsets tableContentInset = self.tableView.contentInset;
  487. UIEdgeInsets tableScrollIndicatorInsets = self.tableView.scrollIndicatorInsets;
  488. _oldBottomTableContentInset = _oldBottomTableContentInset ?: @(tableContentInset.bottom);
  489. if (newBottomInset > [_oldBottomTableContentInset floatValue]){
  490. tableContentInset.bottom = newBottomInset;
  491. tableScrollIndicatorInsets.bottom = tableContentInset.bottom;
  492. [UIView beginAnimations:nil context:nil];
  493. [UIView setAnimationDuration:[keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
  494. [UIView setAnimationCurve:[keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]];
  495. self.tableView.contentInset = tableContentInset;
  496. self.tableView.scrollIndicatorInsets = tableScrollIndicatorInsets;
  497. NSIndexPath *selectedRow = [self.tableView indexPathForCell:cell];
  498. [self.tableView scrollToRowAtIndexPath:selectedRow atScrollPosition:UITableViewScrollPositionNone animated:NO];
  499. [UIView commitAnimations];
  500. }
  501. }
  502. }
  503. - (void)keyboardWillHide:(NSNotification *)notification
  504. {
  505. UIView * firstResponderView = [self.tableView findFirstResponder];
  506. UITableViewCell<XLFormDescriptorCell> * cell = [firstResponderView formDescriptorCell];
  507. if (cell){
  508. _keyboardFrame = CGRectZero;
  509. NSDictionary *keyboardInfo = [notification userInfo];
  510. UIEdgeInsets tableContentInset = self.tableView.contentInset;
  511. UIEdgeInsets tableScrollIndicatorInsets = self.tableView.scrollIndicatorInsets;
  512. tableContentInset.bottom = [_oldBottomTableContentInset floatValue];
  513. tableScrollIndicatorInsets.bottom = tableContentInset.bottom;
  514. _oldBottomTableContentInset = nil;
  515. [UIView beginAnimations:nil context:nil];
  516. [UIView setAnimationDuration:[keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
  517. [UIView setAnimationCurve:[keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]];
  518. self.tableView.contentInset = tableContentInset;
  519. self.tableView.scrollIndicatorInsets = tableScrollIndicatorInsets;
  520. [UIView commitAnimations];
  521. }
  522. }
  523. #pragma mark - Helpers
  524. -(void)deselectFormRow:(XLFormRowDescriptor *)formRow
  525. {
  526. NSIndexPath * indexPath = [self.form indexPathOfFormRow:formRow];
  527. if (indexPath){
  528. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  529. }
  530. }
  531. -(void)reloadFormRow:(XLFormRowDescriptor *)formRow
  532. {
  533. NSIndexPath * indexPath = [self.form indexPathOfFormRow:formRow];
  534. if (indexPath){
  535. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  536. }
  537. }
  538. -(XLFormBaseCell *)updateFormRow:(XLFormRowDescriptor *)formRow
  539. {
  540. XLFormBaseCell * cell = [formRow cellForFormController:self];
  541. [self configureCell:cell];
  542. [cell setNeedsUpdateConstraints];
  543. [cell setNeedsLayout];
  544. return cell;
  545. }
  546. -(void)configureCell:(XLFormBaseCell*) cell
  547. {
  548. [cell update];
  549. [cell.rowDescriptor.cellConfig enumerateKeysAndObjectsUsingBlock:^(NSString *keyPath, id value, BOOL * __unused stop) {
  550. [cell setValue:(value == [NSNull null]) ? nil : value forKeyPath:keyPath];
  551. }];
  552. if (cell.rowDescriptor.isDisabled){
  553. [cell.rowDescriptor.cellConfigIfDisabled enumerateKeysAndObjectsUsingBlock:^(NSString *keyPath, id value, BOOL * __unused stop) {
  554. [cell setValue:(value == [NSNull null]) ? nil : value forKeyPath:keyPath];
  555. }];
  556. }
  557. }
  558. #pragma mark - UITableViewDataSource
  559. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  560. {
  561. return [self.form.formSections count];
  562. }
  563. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  564. {
  565. if (section >= self.form.formSections.count){
  566. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"" userInfo:nil];
  567. }
  568. return [[[self.form.formSections objectAtIndex:section] formRows] count];
  569. }
  570. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  571. {
  572. XLFormRowDescriptor * rowDescriptor = [self.form formRowAtIndex:indexPath];
  573. [self updateFormRow:rowDescriptor];
  574. return [rowDescriptor cellForFormController:self];
  575. }
  576. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  577. {
  578. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  579. if (rowDescriptor.isDisabled || !rowDescriptor.sectionDescriptor.isMultivaluedSection){
  580. return NO;
  581. }
  582. XLFormBaseCell * baseCell = [rowDescriptor cellForFormController:self];
  583. if ([baseCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)baseCell).inlineRowDescriptor){
  584. return NO;
  585. }
  586. return YES;
  587. }
  588. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  589. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  590. XLFormSectionDescriptor * section = rowDescriptor.sectionDescriptor;
  591. if (section.sectionOptions & XLFormSectionOptionCanReorder && section.formRows.count > 1) {
  592. if (section.sectionInsertMode == XLFormSectionInsertModeButton && section.sectionOptions & XLFormSectionOptionCanInsert){
  593. if (section.formRows.count <= 2 || rowDescriptor == section.multivaluedAddButton){
  594. return NO;
  595. }
  596. }
  597. XLFormBaseCell * baseCell = [rowDescriptor cellForFormController:self];
  598. return !([baseCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)baseCell).inlineRowDescriptor);
  599. }
  600. return NO;
  601. }
  602. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  603. {
  604. XLFormRowDescriptor * row = [self.form formRowAtIndex:sourceIndexPath];
  605. XLFormSectionDescriptor * section = row.sectionDescriptor;
  606. #pragma GCC diagnostic push
  607. #pragma GCC diagnostic ignored "-Warc-performSelector-leaks"
  608. [section performSelector:NSSelectorFromString(@"moveRowAtIndexPath:toIndexPath:") withObject:sourceIndexPath withObject:destinationIndexPath];
  609. #pragma GCC diagnostic pop
  610. // update the accessory view
  611. [self inputAccessoryViewForRowDescriptor:row];
  612. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  613. self.tableView.editing = !self.tableView.editing;
  614. self.tableView.editing = !self.tableView.editing;
  615. });
  616. }
  617. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  618. {
  619. if (editingStyle == UITableViewCellEditingStyleDelete){
  620. XLFormRowDescriptor * multivaluedFormRow = [self.form formRowAtIndex:indexPath];
  621. // end editing
  622. UIView * firstResponder = [[multivaluedFormRow cellForFormController:self] findFirstResponder];
  623. if (firstResponder){
  624. [self.tableView endEditing:YES];
  625. }
  626. [multivaluedFormRow.sectionDescriptor removeFormRowAtIndex:indexPath.row];
  627. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  628. self.tableView.editing = !self.tableView.editing;
  629. self.tableView.editing = !self.tableView.editing;
  630. });
  631. if (firstResponder){
  632. UITableViewCell<XLFormDescriptorCell> * firstResponderCell = [firstResponder formDescriptorCell];
  633. XLFormRowDescriptor * rowDescriptor = firstResponderCell.rowDescriptor;
  634. [self inputAccessoryViewForRowDescriptor:rowDescriptor];
  635. }
  636. }
  637. else if (editingStyle == UITableViewCellEditingStyleInsert){
  638. XLFormSectionDescriptor * multivaluedFormSection = [self.form formSectionAtIndex:indexPath.section];
  639. if (multivaluedFormSection.sectionInsertMode == XLFormSectionInsertModeButton && multivaluedFormSection.sectionOptions & XLFormSectionOptionCanInsert){
  640. [self multivaluedInsertButtonTapped:multivaluedFormSection.multivaluedAddButton];
  641. }
  642. else{
  643. XLFormRowDescriptor * formRowDescriptor = [self formRowFormMultivaluedFormSection:multivaluedFormSection];
  644. [multivaluedFormSection addFormRow:formRowDescriptor];
  645. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  646. self.tableView.editing = !self.tableView.editing;
  647. self.tableView.editing = !self.tableView.editing;
  648. });
  649. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  650. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[formRowDescriptor cellForFormController:self];
  651. if ([cell formDescriptorCellCanBecomeFirstResponder]){
  652. [cell formDescriptorCellBecomeFirstResponder];
  653. }
  654. }
  655. }
  656. }
  657. #pragma mark - UITableViewDelegate
  658. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  659. {
  660. return [[self.form.formSections objectAtIndex:section] title];
  661. }
  662. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  663. {
  664. return [[self.form.formSections objectAtIndex:section] footerTitle];
  665. }
  666. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  667. {
  668. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  669. [rowDescriptor cellForFormController:self];
  670. CGFloat height = rowDescriptor.height;
  671. if (height != XLFormUnspecifiedCellHeight){
  672. return height;
  673. }
  674. return self.tableView.rowHeight;
  675. }
  676. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  677. {
  678. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  679. [rowDescriptor cellForFormController:self];
  680. CGFloat height = rowDescriptor.height;
  681. if (height != XLFormUnspecifiedCellHeight){
  682. return height;
  683. }
  684. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
  685. return self.tableView.estimatedRowHeight;
  686. }
  687. return 44;
  688. }
  689. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  690. {
  691. XLFormRowDescriptor * row = [self.form formRowAtIndex:indexPath];
  692. if (row.isDisabled) {
  693. return;
  694. }
  695. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[row cellForFormController:self];
  696. if (!([cell formDescriptorCellCanBecomeFirstResponder] && [cell formDescriptorCellBecomeFirstResponder])){
  697. [self.tableView endEditing:YES];
  698. }
  699. [self didSelectFormRow:row];
  700. }
  701. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  702. {
  703. XLFormRowDescriptor * row = [self.form formRowAtIndex:indexPath];
  704. XLFormSectionDescriptor * section = row.sectionDescriptor;
  705. if (section.sectionOptions & XLFormSectionOptionCanInsert){
  706. if (section.formRows.count == indexPath.row + 2){
  707. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:row.rowType]){
  708. UITableViewCell<XLFormDescriptorCell> * cell = [row cellForFormController:self];
  709. UIView * firstResponder = [cell findFirstResponder];
  710. if (firstResponder){
  711. return UITableViewCellEditingStyleInsert;
  712. }
  713. }
  714. }
  715. else if (section.formRows.count == (indexPath.row + 1)){
  716. return UITableViewCellEditingStyleInsert;
  717. }
  718. }
  719. if (section.sectionOptions & XLFormSectionOptionCanDelete){
  720. return UITableViewCellEditingStyleDelete;
  721. }
  722. return UITableViewCellEditingStyleNone;
  723. }
  724. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
  725. toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
  726. {
  727. if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
  728. return sourceIndexPath;
  729. }
  730. XLFormSectionDescriptor * sectionDescriptor = [self.form formSectionAtIndex:sourceIndexPath.section];
  731. XLFormRowDescriptor * proposedDestination = [sectionDescriptor.formRows objectAtIndex:proposedDestinationIndexPath.row];
  732. XLFormBaseCell * proposedDestinationCell = [proposedDestination cellForFormController:self];
  733. if (([proposedDestinationCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)proposedDestinationCell).inlineRowDescriptor) || ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:proposedDestinationCell.rowDescriptor.rowType] && [[proposedDestinationCell findFirstResponder] formDescriptorCell] == proposedDestinationCell)) {
  734. if (sourceIndexPath.row < proposedDestinationIndexPath.row){
  735. return [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row + 1 inSection:sourceIndexPath.section];
  736. }
  737. else{
  738. return [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row - 1 inSection:sourceIndexPath.section];
  739. }
  740. }
  741. if ((sectionDescriptor.sectionInsertMode == XLFormSectionInsertModeButton && sectionDescriptor.sectionOptions & XLFormSectionOptionCanInsert)){
  742. if (proposedDestinationIndexPath.row == sectionDescriptor.formRows.count - 1){
  743. return [NSIndexPath indexPathForRow:(sectionDescriptor.formRows.count - 2) inSection:sourceIndexPath.section];
  744. }
  745. }
  746. return proposedDestinationIndexPath;
  747. }
  748. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  749. {
  750. UITableViewCellEditingStyle editingStyle = [self tableView:tableView editingStyleForRowAtIndexPath:indexPath];
  751. if (editingStyle == UITableViewCellEditingStyleNone){
  752. return NO;
  753. }
  754. return YES;
  755. }
  756. - (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath
  757. {
  758. // end editing if inline cell is first responder
  759. UITableViewCell<XLFormDescriptorCell> * cell = [[self.tableView findFirstResponder] formDescriptorCell];
  760. if ([[self.form indexPathOfFormRow:cell.rowDescriptor] isEqual:indexPath]){
  761. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:cell.rowDescriptor.rowType]){
  762. [self.tableView endEditing:YES];
  763. }
  764. }
  765. }
  766. #pragma mark - UITextFieldDelegate
  767. - (BOOL)textFieldShouldClear:(UITextField *)textField
  768. {
  769. return YES;
  770. }
  771. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  772. {
  773. // called when 'return' key pressed. return NO to ignore.
  774. UITableViewCell<XLFormDescriptorCell> * cell = [textField formDescriptorCell];
  775. XLFormRowDescriptor * currentRow = cell.rowDescriptor;
  776. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:currentRow
  777. withDirection:XLFormRowNavigationDirectionNext];
  778. if (nextRow){
  779. UITableViewCell<XLFormDescriptorCell> * nextCell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  780. if ([nextCell formDescriptorCellCanBecomeFirstResponder]){
  781. [nextCell formDescriptorCellBecomeFirstResponder];
  782. return YES;
  783. }
  784. }
  785. [self.tableView endEditing:YES];
  786. return YES;
  787. }
  788. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  789. {
  790. UITableViewCell<XLFormDescriptorCell>* cell = textField.formDescriptorCell;
  791. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:textField.formDescriptorCell.rowDescriptor
  792. withDirection:XLFormRowNavigationDirectionNext];
  793. if ([cell conformsToProtocol:@protocol(XLFormReturnKeyProtocol)]) {
  794. textField.returnKeyType = nextRow ? ((id<XLFormReturnKeyProtocol>)cell).nextReturnKeyType : ((id<XLFormReturnKeyProtocol>)cell).returnKeyType;
  795. }
  796. else {
  797. textField.returnKeyType = nextRow ? UIReturnKeyNext : UIReturnKeyDefault;
  798. }
  799. return YES;
  800. }
  801. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  802. {
  803. return YES;
  804. }
  805. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  806. return YES;
  807. }
  808. - (void)textFieldDidBeginEditing:(UITextField *)textField
  809. {
  810. }
  811. -(void)textFieldDidEndEditing:(UITextField *)textField
  812. {
  813. }
  814. #pragma mark - UITextViewDelegate
  815. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  816. {
  817. return YES;
  818. }
  819. -(void)textViewDidBeginEditing:(UITextView *)textView
  820. {
  821. }
  822. -(void)textViewDidEndEditing:(UITextView *)textView
  823. {
  824. }
  825. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  826. return YES;
  827. }
  828. #pragma mark - UIScrollViewDelegate
  829. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  830. {
  831. //dismiss keyboard
  832. if (NO == self.form.endEditingTableViewOnScroll) {
  833. return;
  834. }
  835. UIView * firstResponder = [self.tableView findFirstResponder];
  836. if ([firstResponder conformsToProtocol:@protocol(XLFormDescriptorCell)]){
  837. id<XLFormDescriptorCell> cell = (id<XLFormDescriptorCell>)firstResponder;
  838. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:cell.rowDescriptor.rowType]){
  839. return;
  840. }
  841. }
  842. [self.tableView endEditing:YES];
  843. }
  844. #pragma mark - Segue
  845. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  846. {
  847. if ([sender isKindOfClass:[XLFormRowDescriptor class]]){
  848. UIViewController * destinationViewController = segue.destinationViewController;
  849. XLFormRowDescriptor * rowDescriptor = (XLFormRowDescriptor *)sender;
  850. if (rowDescriptor.rowType == XLFormRowDescriptorTypeSelectorPush || rowDescriptor.rowType == XLFormRowDescriptorTypeSelectorPopover){
  851. NSAssert([destinationViewController conformsToProtocol:@protocol(XLFormRowDescriptorViewController)], @"Segue destinationViewController must conform to XLFormRowDescriptorViewController protocol");
  852. UIViewController<XLFormRowDescriptorViewController> * rowDescriptorViewController = (UIViewController<XLFormRowDescriptorViewController> *)destinationViewController;
  853. rowDescriptorViewController.rowDescriptor = rowDescriptor;
  854. }
  855. else if ([destinationViewController conformsToProtocol:@protocol(XLFormRowDescriptorViewController)]){
  856. UIViewController<XLFormRowDescriptorViewController> * rowDescriptorViewController = (UIViewController<XLFormRowDescriptorViewController> *)destinationViewController;
  857. rowDescriptorViewController.rowDescriptor = rowDescriptor;
  858. }
  859. }
  860. }
  861. #pragma mark - Navigation Between Fields
  862. -(void)rowNavigationAction:(UIBarButtonItem *)sender
  863. {
  864. [self navigateToDirection:(sender == self.navigationAccessoryView.nextButton ? XLFormRowNavigationDirectionNext : XLFormRowNavigationDirectionPrevious)];
  865. }
  866. -(void)rowNavigationDone:(UIBarButtonItem *)sender
  867. {
  868. [self.tableView endEditing:YES];
  869. }
  870. -(void)navigateToDirection:(XLFormRowNavigationDirection)direction
  871. {
  872. UIView * firstResponder = [self.tableView findFirstResponder];
  873. UITableViewCell<XLFormDescriptorCell> * currentCell = [firstResponder formDescriptorCell];
  874. NSIndexPath * currentIndexPath = [self.tableView indexPathForCell:currentCell];
  875. XLFormRowDescriptor * currentRow = [self.form formRowAtIndex:currentIndexPath];
  876. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:currentRow withDirection:direction];
  877. if (nextRow) {
  878. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  879. if ([cell formDescriptorCellCanBecomeFirstResponder]){
  880. NSIndexPath * indexPath = [self.form indexPathOfFormRow:nextRow];
  881. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
  882. [cell formDescriptorCellBecomeFirstResponder];
  883. }
  884. }
  885. }
  886. -(XLFormRowDescriptor *)nextRowDescriptorForRow:(XLFormRowDescriptor*)currentRow withDirection:(XLFormRowNavigationDirection)direction
  887. {
  888. if (!currentRow || (self.form.rowNavigationOptions & XLFormRowNavigationOptionEnabled) != XLFormRowNavigationOptionEnabled) {
  889. return nil;
  890. }
  891. XLFormRowDescriptor * nextRow = (direction == XLFormRowNavigationDirectionNext) ? [self.form nextRowDescriptorForRow:currentRow] : [self.form previousRowDescriptorForRow:currentRow];
  892. if (!nextRow) {
  893. return nil;
  894. }
  895. if ([[nextRow cellForFormController:self] conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)]) {
  896. id<XLFormInlineRowDescriptorCell> inlineCell = (id<XLFormInlineRowDescriptorCell>)[nextRow cellForFormController:self];
  897. if (inlineCell.inlineRowDescriptor){
  898. return [self nextRowDescriptorForRow:nextRow withDirection:direction];
  899. }
  900. }
  901. XLFormRowNavigationOptions rowNavigationOptions = self.form.rowNavigationOptions;
  902. if (nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionStopDisableRow) == XLFormRowNavigationOptionStopDisableRow)){
  903. return nil;
  904. }
  905. if (!nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionStopInlineRow) == XLFormRowNavigationOptionStopInlineRow) && [[[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes] allKeys] containsObject:nextRow.rowType]){
  906. return nil;
  907. }
  908. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  909. if (!nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionSkipCanNotBecomeFirstResponderRow) != XLFormRowNavigationOptionSkipCanNotBecomeFirstResponderRow) && (![cell formDescriptorCellCanBecomeFirstResponder])){
  910. return nil;
  911. }
  912. if (!nextRow.isDisabled && [cell formDescriptorCellCanBecomeFirstResponder]){
  913. return nextRow;
  914. }
  915. return [self nextRowDescriptorForRow:nextRow withDirection:direction];
  916. }
  917. #pragma mark - properties
  918. -(void)setForm:(XLFormDescriptor *)form
  919. {
  920. _form.delegate = nil;
  921. [self.tableView endEditing:YES];
  922. _form = form;
  923. _form.delegate = self;
  924. [_form forceEvaluate];
  925. if ([self isViewLoaded]){
  926. [self.tableView reloadData];
  927. }
  928. }
  929. -(XLFormDescriptor *)form
  930. {
  931. return _form;
  932. }
  933. -(XLFormRowNavigationAccessoryView *)navigationAccessoryView
  934. {
  935. if (_navigationAccessoryView) return _navigationAccessoryView;
  936. _navigationAccessoryView = [XLFormRowNavigationAccessoryView new];
  937. _navigationAccessoryView.previousButton.target = self;
  938. _navigationAccessoryView.previousButton.action = @selector(rowNavigationAction:);
  939. _navigationAccessoryView.nextButton.target = self;
  940. _navigationAccessoryView.nextButton.action = @selector(rowNavigationAction:);
  941. _navigationAccessoryView.doneButton.target = self;
  942. _navigationAccessoryView.doneButton.action = @selector(rowNavigationDone:);
  943. _navigationAccessoryView.tintColor = self.view.tintColor;
  944. return _navigationAccessoryView;
  945. }
  946. @end