FLEXPropertyEditorViewController.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // FLEXPropertyEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/20/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXPropertyEditorViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXArgumentInputView.h"
  12. @interface FLEXPropertyEditorViewController ()
  13. @property (nonatomic, assign) objc_property_t property;
  14. @end
  15. @implementation FLEXPropertyEditorViewController
  16. - (id)initWithTarget:(id)target property:(objc_property_t)property
  17. {
  18. self = [super initWithTarget:target];
  19. if (self) {
  20. self.property = property;
  21. self.title = @"Property";
  22. }
  23. return self;
  24. }
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility fullDescriptionForProperty:self.property];
  29. id currentValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  30. self.setterButton.enabled = [[self class] canEditProperty:self.property currentValue:currentValue];
  31. [self updateTextFieldString];
  32. // Use the numeric keyboard for primitives and the letter keyboard for strings
  33. NSString *typeEncoding = [FLEXRuntimeUtility typeEncodingForProperty:self.property];
  34. if ([typeEncoding isEqual:[[self class] stringTypeEncoding]]) {
  35. self.firstInputView.keyboardType = UIKeyboardTypeAlphabet;
  36. } else {
  37. self.firstInputView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
  38. }
  39. }
  40. - (void)actionButtonPressed:(id)sender
  41. {
  42. [super actionButtonPressed:sender];
  43. [self updatePropertyFromString:self.firstInputView.inputText];
  44. [self updateTextFieldString];
  45. }
  46. - (void)updateTextFieldString
  47. {
  48. id propertyValue = [FLEXRuntimeUtility valueForProperty:self.property onObject:self.target];
  49. self.firstInputView.inputText = [FLEXRuntimeUtility editiableDescriptionForObject:propertyValue];
  50. }
  51. - (void)updatePropertyFromString:(NSString *)string
  52. {
  53. SEL setterSelector = [FLEXRuntimeUtility setterSelectorForProperty:self.property];
  54. NSArray *arguments = string ? @[string] : nil;
  55. [FLEXRuntimeUtility performSelector:setterSelector onObject:self.target withArguments:arguments error:NULL];
  56. }
  57. + (BOOL)canEditProperty:(objc_property_t)property currentValue:(id)value
  58. {
  59. BOOL canEditType = [self canEditType:[FLEXRuntimeUtility typeEncodingForProperty:property] currentObjectValue:value];
  60. BOOL isReadonly = [FLEXRuntimeUtility isReadonlyProperty:property];
  61. return canEditType && !isReadonly;
  62. }
  63. @end