XLFormViewController.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"XLFormViewController_ValidationErrorTitle", nil)
  404. message:error.localizedDescription
  405. preferredStyle:UIAlertControllerStyleAlert];
  406. [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
  407. style:UIAlertActionStyleDefault
  408. handler:nil]];
  409. [self presentViewController:alertController animated:YES completion:nil];
  410. }
  411. -(void)showFormValidationError:(NSError *)error withTitle:(NSString*)title
  412. {
  413. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(title, nil)
  414. message:error.localizedDescription
  415. preferredStyle:UIAlertControllerStyleAlert];
  416. [alertController addAction:[UIAlertAction actionWithTitle:@"OK"
  417. style:UIAlertActionStyleDefault
  418. handler:nil]];
  419. [self presentViewController:alertController animated:YES completion:nil];
  420. }
  421. -(void)performFormSelector:(SEL)selector withObject:(id)sender
  422. {
  423. UIResponder * responder = [self targetForAction:selector withSender:sender];;
  424. if (responder) {
  425. #pragma GCC diagnostic push
  426. #pragma GCC diagnostic ignored "-Warc-performSelector-leaks"
  427. [responder performSelector:selector withObject:sender];
  428. #pragma GCC diagnostic pop
  429. }
  430. }
  431. #pragma mark - Private
  432. - (void)contentSizeCategoryChanged:(NSNotification *)notification
  433. {
  434. [self.tableView reloadData];
  435. }
  436. - (void)keyboardWillShow:(NSNotification *)notification
  437. {
  438. UIView * firstResponderView = [self.tableView findFirstResponder];
  439. UITableViewCell<XLFormDescriptorCell> * cell = [firstResponderView formDescriptorCell];
  440. if (cell){
  441. NSDictionary *keyboardInfo = [notification userInfo];
  442. _keyboardFrame = [self.tableView.window convertRect:[keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:self.tableView.superview];
  443. CGFloat newBottomInset = self.tableView.frame.origin.y + self.tableView.frame.size.height - _keyboardFrame.origin.y;
  444. UIEdgeInsets tableContentInset = self.tableView.contentInset;
  445. UIEdgeInsets tableScrollIndicatorInsets = self.tableView.scrollIndicatorInsets;
  446. _oldBottomTableContentInset = _oldBottomTableContentInset ?: @(tableContentInset.bottom);
  447. if (newBottomInset > [_oldBottomTableContentInset floatValue]){
  448. tableContentInset.bottom = newBottomInset;
  449. tableScrollIndicatorInsets.bottom = tableContentInset.bottom;
  450. [UIView beginAnimations:nil context:nil];
  451. [UIView setAnimationDuration:[keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
  452. [UIView setAnimationCurve:[keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]];
  453. self.tableView.contentInset = tableContentInset;
  454. self.tableView.scrollIndicatorInsets = tableScrollIndicatorInsets;
  455. NSIndexPath *selectedRow = [self.tableView indexPathForCell:cell];
  456. [self.tableView scrollToRowAtIndexPath:selectedRow atScrollPosition:UITableViewScrollPositionNone animated:NO];
  457. [UIView commitAnimations];
  458. }
  459. }
  460. }
  461. - (void)keyboardWillHide:(NSNotification *)notification
  462. {
  463. UIView * firstResponderView = [self.tableView findFirstResponder];
  464. UITableViewCell<XLFormDescriptorCell> * cell = [firstResponderView formDescriptorCell];
  465. if (cell){
  466. _keyboardFrame = CGRectZero;
  467. NSDictionary *keyboardInfo = [notification userInfo];
  468. UIEdgeInsets tableContentInset = self.tableView.contentInset;
  469. UIEdgeInsets tableScrollIndicatorInsets = self.tableView.scrollIndicatorInsets;
  470. tableContentInset.bottom = [_oldBottomTableContentInset floatValue];
  471. tableScrollIndicatorInsets.bottom = tableContentInset.bottom;
  472. _oldBottomTableContentInset = nil;
  473. [UIView beginAnimations:nil context:nil];
  474. [UIView setAnimationDuration:[keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
  475. [UIView setAnimationCurve:[keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] intValue]];
  476. self.tableView.contentInset = tableContentInset;
  477. self.tableView.scrollIndicatorInsets = tableScrollIndicatorInsets;
  478. [UIView commitAnimations];
  479. }
  480. }
  481. #pragma mark - Helpers
  482. -(void)deselectFormRow:(XLFormRowDescriptor *)formRow
  483. {
  484. NSIndexPath * indexPath = [self.form indexPathOfFormRow:formRow];
  485. if (indexPath){
  486. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  487. }
  488. }
  489. -(void)reloadFormRow:(XLFormRowDescriptor *)formRow
  490. {
  491. NSIndexPath * indexPath = [self.form indexPathOfFormRow:formRow];
  492. if (indexPath){
  493. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  494. }
  495. }
  496. -(XLFormBaseCell *)updateFormRow:(XLFormRowDescriptor *)formRow
  497. {
  498. XLFormBaseCell * cell = [formRow cellForFormController:self];
  499. [self configureCell:cell];
  500. [cell setNeedsUpdateConstraints];
  501. [cell setNeedsLayout];
  502. return cell;
  503. }
  504. -(void)configureCell:(XLFormBaseCell*) cell
  505. {
  506. [cell update];
  507. [cell.rowDescriptor.cellConfig enumerateKeysAndObjectsUsingBlock:^(NSString *keyPath, id value, BOOL * __unused stop) {
  508. [cell setValue:(value == [NSNull null]) ? nil : value forKeyPath:keyPath];
  509. }];
  510. if (cell.rowDescriptor.isDisabled){
  511. [cell.rowDescriptor.cellConfigIfDisabled enumerateKeysAndObjectsUsingBlock:^(NSString *keyPath, id value, BOOL * __unused stop) {
  512. [cell setValue:(value == [NSNull null]) ? nil : value forKeyPath:keyPath];
  513. }];
  514. }
  515. }
  516. #pragma mark - UITableViewDataSource
  517. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  518. {
  519. return [self.form.formSections count];
  520. }
  521. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  522. {
  523. if (section >= self.form.formSections.count){
  524. @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"" userInfo:nil];
  525. }
  526. return [[[self.form.formSections objectAtIndex:section] formRows] count];
  527. }
  528. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  529. {
  530. XLFormRowDescriptor * rowDescriptor = [self.form formRowAtIndex:indexPath];
  531. [self updateFormRow:rowDescriptor];
  532. return [rowDescriptor cellForFormController:self];
  533. }
  534. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  535. {
  536. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  537. if (rowDescriptor.isDisabled || !rowDescriptor.sectionDescriptor.isMultivaluedSection){
  538. return NO;
  539. }
  540. XLFormBaseCell * baseCell = [rowDescriptor cellForFormController:self];
  541. if ([baseCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)baseCell).inlineRowDescriptor){
  542. return NO;
  543. }
  544. return YES;
  545. }
  546. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  547. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  548. XLFormSectionDescriptor * section = rowDescriptor.sectionDescriptor;
  549. if (section.sectionOptions & XLFormSectionOptionCanReorder && section.formRows.count > 1) {
  550. if (section.sectionInsertMode == XLFormSectionInsertModeButton && section.sectionOptions & XLFormSectionOptionCanInsert){
  551. if (section.formRows.count <= 2 || rowDescriptor == section.multivaluedAddButton){
  552. return NO;
  553. }
  554. }
  555. XLFormBaseCell * baseCell = [rowDescriptor cellForFormController:self];
  556. return !([baseCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)baseCell).inlineRowDescriptor);
  557. }
  558. return NO;
  559. }
  560. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  561. {
  562. XLFormRowDescriptor * row = [self.form formRowAtIndex:sourceIndexPath];
  563. XLFormSectionDescriptor * section = row.sectionDescriptor;
  564. #pragma GCC diagnostic push
  565. #pragma GCC diagnostic ignored "-Warc-performSelector-leaks"
  566. [section performSelector:NSSelectorFromString(@"moveRowAtIndexPath:toIndexPath:") withObject:sourceIndexPath withObject:destinationIndexPath];
  567. #pragma GCC diagnostic pop
  568. // update the accessory view
  569. [self inputAccessoryViewForRowDescriptor:row];
  570. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  571. self.tableView.editing = !self.tableView.editing;
  572. self.tableView.editing = !self.tableView.editing;
  573. });
  574. }
  575. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  576. {
  577. if (editingStyle == UITableViewCellEditingStyleDelete){
  578. XLFormRowDescriptor * multivaluedFormRow = [self.form formRowAtIndex:indexPath];
  579. // end editing
  580. UIView * firstResponder = [[multivaluedFormRow cellForFormController:self] findFirstResponder];
  581. if (firstResponder){
  582. [self.tableView endEditing:YES];
  583. }
  584. [multivaluedFormRow.sectionDescriptor removeFormRowAtIndex:indexPath.row];
  585. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  586. self.tableView.editing = !self.tableView.editing;
  587. self.tableView.editing = !self.tableView.editing;
  588. });
  589. if (firstResponder){
  590. UITableViewCell<XLFormDescriptorCell> * firstResponderCell = [firstResponder formDescriptorCell];
  591. XLFormRowDescriptor * rowDescriptor = firstResponderCell.rowDescriptor;
  592. [self inputAccessoryViewForRowDescriptor:rowDescriptor];
  593. }
  594. }
  595. else if (editingStyle == UITableViewCellEditingStyleInsert){
  596. XLFormSectionDescriptor * multivaluedFormSection = [self.form formSectionAtIndex:indexPath.section];
  597. if (multivaluedFormSection.sectionInsertMode == XLFormSectionInsertModeButton && multivaluedFormSection.sectionOptions & XLFormSectionOptionCanInsert){
  598. [self multivaluedInsertButtonTapped:multivaluedFormSection.multivaluedAddButton];
  599. }
  600. else{
  601. XLFormRowDescriptor * formRowDescriptor = [self formRowFormMultivaluedFormSection:multivaluedFormSection];
  602. [multivaluedFormSection addFormRow:formRowDescriptor];
  603. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.02 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  604. self.tableView.editing = !self.tableView.editing;
  605. self.tableView.editing = !self.tableView.editing;
  606. });
  607. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  608. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[formRowDescriptor cellForFormController:self];
  609. if ([cell formDescriptorCellCanBecomeFirstResponder]){
  610. [cell formDescriptorCellBecomeFirstResponder];
  611. }
  612. }
  613. }
  614. }
  615. #pragma mark - UITableViewDelegate
  616. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  617. {
  618. return [[self.form.formSections objectAtIndex:section] title];
  619. }
  620. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
  621. {
  622. return [[self.form.formSections objectAtIndex:section] footerTitle];
  623. }
  624. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  625. {
  626. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  627. [rowDescriptor cellForFormController:self];
  628. CGFloat height = rowDescriptor.height;
  629. if (height != XLFormUnspecifiedCellHeight){
  630. return height;
  631. }
  632. return self.tableView.rowHeight;
  633. }
  634. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  635. {
  636. XLFormRowDescriptor *rowDescriptor = [self.form formRowAtIndex:indexPath];
  637. [rowDescriptor cellForFormController:self];
  638. CGFloat height = rowDescriptor.height;
  639. if (height != XLFormUnspecifiedCellHeight){
  640. return height;
  641. }
  642. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
  643. return self.tableView.estimatedRowHeight;
  644. }
  645. return 44;
  646. }
  647. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  648. {
  649. XLFormRowDescriptor * row = [self.form formRowAtIndex:indexPath];
  650. if (row.isDisabled) {
  651. return;
  652. }
  653. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[row cellForFormController:self];
  654. if (!([cell formDescriptorCellCanBecomeFirstResponder] && [cell formDescriptorCellBecomeFirstResponder])){
  655. [self.tableView endEditing:YES];
  656. }
  657. [self didSelectFormRow:row];
  658. }
  659. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  660. {
  661. XLFormRowDescriptor * row = [self.form formRowAtIndex:indexPath];
  662. XLFormSectionDescriptor * section = row.sectionDescriptor;
  663. if (section.sectionOptions & XLFormSectionOptionCanInsert){
  664. if (section.formRows.count == indexPath.row + 2){
  665. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:row.rowType]){
  666. UITableViewCell<XLFormDescriptorCell> * cell = [row cellForFormController:self];
  667. UIView * firstResponder = [cell findFirstResponder];
  668. if (firstResponder){
  669. return UITableViewCellEditingStyleInsert;
  670. }
  671. }
  672. }
  673. else if (section.formRows.count == (indexPath.row + 1)){
  674. return UITableViewCellEditingStyleInsert;
  675. }
  676. }
  677. if (section.sectionOptions & XLFormSectionOptionCanDelete){
  678. return UITableViewCellEditingStyleDelete;
  679. }
  680. return UITableViewCellEditingStyleNone;
  681. }
  682. - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
  683. toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
  684. {
  685. if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
  686. return sourceIndexPath;
  687. }
  688. XLFormSectionDescriptor * sectionDescriptor = [self.form formSectionAtIndex:sourceIndexPath.section];
  689. XLFormRowDescriptor * proposedDestination = [sectionDescriptor.formRows objectAtIndex:proposedDestinationIndexPath.row];
  690. XLFormBaseCell * proposedDestinationCell = [proposedDestination cellForFormController:self];
  691. if (([proposedDestinationCell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)] && ((id<XLFormInlineRowDescriptorCell>)proposedDestinationCell).inlineRowDescriptor) || ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:proposedDestinationCell.rowDescriptor.rowType] && [[proposedDestinationCell findFirstResponder] formDescriptorCell] == proposedDestinationCell)) {
  692. if (sourceIndexPath.row < proposedDestinationIndexPath.row){
  693. return [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row + 1 inSection:sourceIndexPath.section];
  694. }
  695. else{
  696. return [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row - 1 inSection:sourceIndexPath.section];
  697. }
  698. }
  699. if ((sectionDescriptor.sectionInsertMode == XLFormSectionInsertModeButton && sectionDescriptor.sectionOptions & XLFormSectionOptionCanInsert)){
  700. if (proposedDestinationIndexPath.row == sectionDescriptor.formRows.count - 1){
  701. return [NSIndexPath indexPathForRow:(sectionDescriptor.formRows.count - 2) inSection:sourceIndexPath.section];
  702. }
  703. }
  704. return proposedDestinationIndexPath;
  705. }
  706. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  707. {
  708. UITableViewCellEditingStyle editingStyle = [self tableView:tableView editingStyleForRowAtIndexPath:indexPath];
  709. if (editingStyle == UITableViewCellEditingStyleNone){
  710. return NO;
  711. }
  712. return YES;
  713. }
  714. - (void)tableView:(UITableView *)tableView willBeginReorderingRowAtIndexPath:(NSIndexPath *)indexPath
  715. {
  716. // end editing if inline cell is first responder
  717. UITableViewCell<XLFormDescriptorCell> * cell = [[self.tableView findFirstResponder] formDescriptorCell];
  718. if ([[self.form indexPathOfFormRow:cell.rowDescriptor] isEqual:indexPath]){
  719. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:cell.rowDescriptor.rowType]){
  720. [self.tableView endEditing:YES];
  721. }
  722. }
  723. }
  724. #pragma mark - UITextFieldDelegate
  725. - (BOOL)textFieldShouldClear:(UITextField *)textField
  726. {
  727. return YES;
  728. }
  729. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  730. {
  731. // called when 'return' key pressed. return NO to ignore.
  732. UITableViewCell<XLFormDescriptorCell> * cell = [textField formDescriptorCell];
  733. XLFormRowDescriptor * currentRow = cell.rowDescriptor;
  734. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:currentRow
  735. withDirection:XLFormRowNavigationDirectionNext];
  736. if (nextRow){
  737. UITableViewCell<XLFormDescriptorCell> * nextCell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  738. if ([nextCell formDescriptorCellCanBecomeFirstResponder]){
  739. [nextCell formDescriptorCellBecomeFirstResponder];
  740. return YES;
  741. }
  742. }
  743. [self.tableView endEditing:YES];
  744. return YES;
  745. }
  746. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  747. {
  748. UITableViewCell<XLFormDescriptorCell>* cell = textField.formDescriptorCell;
  749. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:textField.formDescriptorCell.rowDescriptor
  750. withDirection:XLFormRowNavigationDirectionNext];
  751. if ([cell conformsToProtocol:@protocol(XLFormReturnKeyProtocol)]) {
  752. textField.returnKeyType = nextRow ? ((id<XLFormReturnKeyProtocol>)cell).nextReturnKeyType : ((id<XLFormReturnKeyProtocol>)cell).returnKeyType;
  753. }
  754. else {
  755. textField.returnKeyType = nextRow ? UIReturnKeyNext : UIReturnKeyDefault;
  756. }
  757. return YES;
  758. }
  759. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  760. {
  761. return YES;
  762. }
  763. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  764. return YES;
  765. }
  766. - (void)textFieldDidBeginEditing:(UITextField *)textField
  767. {
  768. }
  769. -(void)textFieldDidEndEditing:(UITextField *)textField
  770. {
  771. }
  772. #pragma mark - UITextViewDelegate
  773. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  774. {
  775. return YES;
  776. }
  777. -(void)textViewDidBeginEditing:(UITextView *)textView
  778. {
  779. }
  780. -(void)textViewDidEndEditing:(UITextView *)textView
  781. {
  782. }
  783. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  784. return YES;
  785. }
  786. #pragma mark - UIScrollViewDelegate
  787. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  788. {
  789. //dismiss keyboard
  790. if (NO == self.form.endEditingTableViewOnScroll) {
  791. return;
  792. }
  793. UIView * firstResponder = [self.tableView findFirstResponder];
  794. if ([firstResponder conformsToProtocol:@protocol(XLFormDescriptorCell)]){
  795. id<XLFormDescriptorCell> cell = (id<XLFormDescriptorCell>)firstResponder;
  796. if ([[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes].allKeys containsObject:cell.rowDescriptor.rowType]){
  797. return;
  798. }
  799. }
  800. [self.tableView endEditing:YES];
  801. }
  802. #pragma mark - Segue
  803. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  804. {
  805. if ([sender isKindOfClass:[XLFormRowDescriptor class]]){
  806. UIViewController * destinationViewController = segue.destinationViewController;
  807. XLFormRowDescriptor * rowDescriptor = (XLFormRowDescriptor *)sender;
  808. if (rowDescriptor.rowType == XLFormRowDescriptorTypeSelectorPush || rowDescriptor.rowType == XLFormRowDescriptorTypeSelectorPopover){
  809. NSAssert([destinationViewController conformsToProtocol:@protocol(XLFormRowDescriptorViewController)], @"Segue destinationViewController must conform to XLFormRowDescriptorViewController protocol");
  810. UIViewController<XLFormRowDescriptorViewController> * rowDescriptorViewController = (UIViewController<XLFormRowDescriptorViewController> *)destinationViewController;
  811. rowDescriptorViewController.rowDescriptor = rowDescriptor;
  812. }
  813. else if ([destinationViewController conformsToProtocol:@protocol(XLFormRowDescriptorViewController)]){
  814. UIViewController<XLFormRowDescriptorViewController> * rowDescriptorViewController = (UIViewController<XLFormRowDescriptorViewController> *)destinationViewController;
  815. rowDescriptorViewController.rowDescriptor = rowDescriptor;
  816. }
  817. }
  818. }
  819. #pragma mark - Navigation Between Fields
  820. -(void)rowNavigationAction:(UIBarButtonItem *)sender
  821. {
  822. [self navigateToDirection:(sender == self.navigationAccessoryView.nextButton ? XLFormRowNavigationDirectionNext : XLFormRowNavigationDirectionPrevious)];
  823. }
  824. -(void)rowNavigationDone:(UIBarButtonItem *)sender
  825. {
  826. [self.tableView endEditing:YES];
  827. }
  828. -(void)navigateToDirection:(XLFormRowNavigationDirection)direction
  829. {
  830. UIView * firstResponder = [self.tableView findFirstResponder];
  831. UITableViewCell<XLFormDescriptorCell> * currentCell = [firstResponder formDescriptorCell];
  832. NSIndexPath * currentIndexPath = [self.tableView indexPathForCell:currentCell];
  833. XLFormRowDescriptor * currentRow = [self.form formRowAtIndex:currentIndexPath];
  834. XLFormRowDescriptor * nextRow = [self nextRowDescriptorForRow:currentRow withDirection:direction];
  835. if (nextRow) {
  836. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  837. if ([cell formDescriptorCellCanBecomeFirstResponder]){
  838. NSIndexPath * indexPath = [self.form indexPathOfFormRow:nextRow];
  839. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
  840. [cell formDescriptorCellBecomeFirstResponder];
  841. }
  842. }
  843. }
  844. -(XLFormRowDescriptor *)nextRowDescriptorForRow:(XLFormRowDescriptor*)currentRow withDirection:(XLFormRowNavigationDirection)direction
  845. {
  846. if (!currentRow || (self.form.rowNavigationOptions & XLFormRowNavigationOptionEnabled) != XLFormRowNavigationOptionEnabled) {
  847. return nil;
  848. }
  849. XLFormRowDescriptor * nextRow = (direction == XLFormRowNavigationDirectionNext) ? [self.form nextRowDescriptorForRow:currentRow] : [self.form previousRowDescriptorForRow:currentRow];
  850. if (!nextRow) {
  851. return nil;
  852. }
  853. if ([[nextRow cellForFormController:self] conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)]) {
  854. id<XLFormInlineRowDescriptorCell> inlineCell = (id<XLFormInlineRowDescriptorCell>)[nextRow cellForFormController:self];
  855. if (inlineCell.inlineRowDescriptor){
  856. return [self nextRowDescriptorForRow:nextRow withDirection:direction];
  857. }
  858. }
  859. XLFormRowNavigationOptions rowNavigationOptions = self.form.rowNavigationOptions;
  860. if (nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionStopDisableRow) == XLFormRowNavigationOptionStopDisableRow)){
  861. return nil;
  862. }
  863. if (!nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionStopInlineRow) == XLFormRowNavigationOptionStopInlineRow) && [[[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes] allKeys] containsObject:nextRow.rowType]){
  864. return nil;
  865. }
  866. UITableViewCell<XLFormDescriptorCell> * cell = (UITableViewCell<XLFormDescriptorCell> *)[nextRow cellForFormController:self];
  867. if (!nextRow.isDisabled && ((rowNavigationOptions & XLFormRowNavigationOptionSkipCanNotBecomeFirstResponderRow) != XLFormRowNavigationOptionSkipCanNotBecomeFirstResponderRow) && (![cell formDescriptorCellCanBecomeFirstResponder])){
  868. return nil;
  869. }
  870. if (!nextRow.isDisabled && [cell formDescriptorCellCanBecomeFirstResponder]){
  871. return nextRow;
  872. }
  873. return [self nextRowDescriptorForRow:nextRow withDirection:direction];
  874. }
  875. #pragma mark - properties
  876. -(void)setForm:(XLFormDescriptor *)form
  877. {
  878. _form.delegate = nil;
  879. [self.tableView endEditing:YES];
  880. _form = form;
  881. _form.delegate = self;
  882. [_form forceEvaluate];
  883. if ([self isViewLoaded]){
  884. [self.tableView reloadData];
  885. }
  886. }
  887. -(XLFormDescriptor *)form
  888. {
  889. return _form;
  890. }
  891. -(XLFormRowNavigationAccessoryView *)navigationAccessoryView
  892. {
  893. if (_navigationAccessoryView) return _navigationAccessoryView;
  894. _navigationAccessoryView = [XLFormRowNavigationAccessoryView new];
  895. _navigationAccessoryView.previousButton.target = self;
  896. _navigationAccessoryView.previousButton.action = @selector(rowNavigationAction:);
  897. _navigationAccessoryView.nextButton.target = self;
  898. _navigationAccessoryView.nextButton.action = @selector(rowNavigationAction:);
  899. _navigationAccessoryView.doneButton.target = self;
  900. _navigationAccessoryView.doneButton.action = @selector(rowNavigationDone:);
  901. _navigationAccessoryView.tintColor = self.view.tintColor;
  902. return _navigationAccessoryView;
  903. }
  904. @end