FLEXMethodCallingViewController.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // FLEXMethodCallingViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXMethodCallingViewController.h"
  9. #import "FLEXRuntimeUtility.h"
  10. #import "FLEXFieldEditorView.h"
  11. #import "FLEXObjectExplorerFactory.h"
  12. #import "FLEXObjectExplorerViewController.h"
  13. #import "FLEXArgumentInputView.h"
  14. #import "FLEXArgumentInputViewFactory.h"
  15. @interface FLEXMethodCallingViewController ()
  16. @property (nonatomic, assign) Method method;
  17. @end
  18. @implementation FLEXMethodCallingViewController
  19. - (id)initWithTarget:(id)target method:(Method)method
  20. {
  21. self = [super initWithTarget:target];
  22. if (self) {
  23. self.method = method;
  24. self.title = [self isClassMethod] ? @"Class Method" : @"Method";
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility prettyNameForMethod:self.method isClassMethod:[self isClassMethod]];
  32. NSArray *methodComponents = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:self.method];
  33. NSMutableArray *argumentInputViews = [NSMutableArray array];
  34. unsigned int argumentIndex = kFLEXNumberOfImplicitArgs;
  35. for (NSString *methodComponent in methodComponents) {
  36. char *argumentTypeEncoding = method_copyArgumentType(self.method, argumentIndex);
  37. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:argumentTypeEncoding];
  38. free(argumentTypeEncoding);
  39. inputView.backgroundColor = self.view.backgroundColor;
  40. inputView.title = methodComponent;
  41. [argumentInputViews addObject:inputView];
  42. argumentIndex++;
  43. }
  44. self.fieldEditorView.argumentInputViews = argumentInputViews;
  45. }
  46. - (BOOL)isClassMethod
  47. {
  48. return self.target && self.target == [self.target class];
  49. }
  50. - (NSString *)titleForActionButton
  51. {
  52. return @"Call";
  53. }
  54. - (void)actionButtonPressed:(id)sender
  55. {
  56. [super actionButtonPressed:sender];
  57. NSMutableArray *arguments = [NSMutableArray array];
  58. for (FLEXArgumentInputView *inputView in self.fieldEditorView.argumentInputViews) {
  59. id argumentValue = inputView.inputValue;
  60. if (!argumentValue) {
  61. // Use NSNulls as placeholders in the array. They will be interpreted as nil arguments.
  62. argumentValue = [NSNull null];
  63. }
  64. [arguments addObject:argumentValue];
  65. }
  66. id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:NULL];
  67. // For non-nil (or void) return types, push an explorer view controller to display the returned object
  68. if (returnedObject) {
  69. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:returnedObject];
  70. [self.navigationController pushViewController:explorerViewController animated:YES];
  71. }
  72. }
  73. @end