XLFormButtonCell.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // XLFormButtonCell.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 "XLFormRowDescriptor.h"
  26. #import "XLFormButtonCell.h"
  27. @implementation XLFormButtonCell
  28. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  29. {
  30. return [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  31. }
  32. #pragma mark - XLFormDescriptorCell
  33. -(void)configure
  34. {
  35. [super configure];
  36. }
  37. -(void)update
  38. {
  39. [super update];
  40. BOOL isDisabled = self.rowDescriptor.isDisabled;
  41. self.textLabel.text = self.rowDescriptor.title;
  42. BOOL simpleAction = !(self.rowDescriptor.action.viewControllerClass || [self.rowDescriptor.action.viewControllerStoryboardId length] != 0 || [self.rowDescriptor.action.viewControllerNibName length] != 0 || [self.rowDescriptor.action.formSegueIdentifier length] != 0 || self.rowDescriptor.action.formSegueClass);
  43. self.textLabel.textAlignment = !simpleAction ? NSTextAlignmentNatural : NSTextAlignmentCenter;
  44. self.accessoryType = simpleAction || isDisabled ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;
  45. self.editingAccessoryType = self.accessoryType;
  46. self.selectionStyle = isDisabled ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
  47. if (simpleAction){
  48. CGFloat red, green, blue, alpha;
  49. [self.tintColor getRed:&red green:&green blue:&blue alpha:&alpha];
  50. self.textLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:(isDisabled ? 0.3 : 1.0)];
  51. }
  52. self.detailTextLabel.text = self.rowDescriptor.value;
  53. }
  54. -(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  55. {
  56. if (self.rowDescriptor.action.formBlock){
  57. self.rowDescriptor.action.formBlock(self.rowDescriptor);
  58. }
  59. else if (self.rowDescriptor.action.formSelector){
  60. [controller performFormSelector:self.rowDescriptor.action.formSelector withObject:self.rowDescriptor];
  61. }
  62. else if ([self.rowDescriptor.action.formSegueIdentifier length] != 0){
  63. [controller performSegueWithIdentifier:self.rowDescriptor.action.formSegueIdentifier sender:self.rowDescriptor];
  64. }
  65. else if (self.rowDescriptor.action.formSegueClass){
  66. UIViewController * controllerToPresent = [self controllerToPresent];
  67. NSAssert(controllerToPresent, @"either rowDescriptor.action.viewControllerClass or rowDescriptor.action.viewControllerStoryboardId or rowDescriptor.action.viewControllerNibName must be assigned");
  68. UIStoryboardSegue * segue = [[self.rowDescriptor.action.formSegueClass alloc] initWithIdentifier:self.rowDescriptor.tag source:controller destination:controllerToPresent];
  69. [controller prepareForSegue:segue sender:self.rowDescriptor];
  70. [segue perform];
  71. }
  72. else{
  73. UIViewController * controllerToPresent = [self controllerToPresent];
  74. if (controllerToPresent){
  75. if ([controllerToPresent conformsToProtocol:@protocol(XLFormRowDescriptorViewController)]){
  76. ((UIViewController<XLFormRowDescriptorViewController> *)controllerToPresent).rowDescriptor = self.rowDescriptor;
  77. }
  78. if (controller.navigationController == nil || [controllerToPresent isKindOfClass:[UINavigationController class]] || self.rowDescriptor.action.viewControllerPresentationMode == XLFormPresentationModePresent){
  79. [controller presentViewController:controllerToPresent animated:YES completion:nil];
  80. }
  81. else{
  82. [controller.navigationController pushViewController:controllerToPresent animated:YES];
  83. }
  84. }
  85. }
  86. }
  87. #pragma mark - Helpers
  88. -(UIViewController *)controllerToPresent
  89. {
  90. if (self.rowDescriptor.action.viewControllerClass){
  91. return [[self.rowDescriptor.action.viewControllerClass alloc] init];
  92. }
  93. else if ([self.rowDescriptor.action.viewControllerStoryboardId length] != 0){
  94. UIStoryboard * storyboard = [self storyboardToPresent];
  95. NSAssert(storyboard != nil, @"You must provide a storyboard when rowDescriptor.action.viewControllerStoryboardId is used");
  96. return [storyboard instantiateViewControllerWithIdentifier:self.rowDescriptor.action.viewControllerStoryboardId];
  97. }
  98. else if ([self.rowDescriptor.action.viewControllerNibName length] != 0){
  99. Class viewControllerClass = NSClassFromString(self.rowDescriptor.action.viewControllerNibName);
  100. NSAssert(viewControllerClass, @"class owner of self.rowDescriptor.action.viewControllerNibName must be equal to %@", self.rowDescriptor.action.viewControllerNibName);
  101. return [[viewControllerClass alloc] initWithNibName:self.rowDescriptor.action.viewControllerNibName bundle:nil];
  102. }
  103. return nil;
  104. }
  105. -(UIStoryboard *)storyboardToPresent
  106. {
  107. if ([self.formViewController respondsToSelector:@selector(storyboardForRow:)]){
  108. return [self.formViewController storyboardForRow:self.rowDescriptor];
  109. }
  110. if (self.formViewController.storyboard){
  111. return self.formViewController.storyboard;
  112. }
  113. return nil;
  114. }
  115. @end