FLEXMethodCallingViewController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "FLEXArgumentInputView.h"
  12. #import "FLEXArgumentInputViewFactory.h"
  13. @interface FLEXMethodCallingViewController ()
  14. @property (nonatomic, assign) Method method;
  15. @end
  16. @implementation FLEXMethodCallingViewController
  17. - (id)initWithTarget:(id)target method:(Method)method
  18. {
  19. self = [super initWithTarget:target];
  20. if (self) {
  21. self.method = method;
  22. self.title = [self isClassMethod] ? @"Class Method" : @"Method";
  23. }
  24. return self;
  25. }
  26. - (void)viewDidLoad
  27. {
  28. [super viewDidLoad];
  29. self.fieldEditorView.fieldDescription = [FLEXRuntimeUtility prettyNameForMethod:self.method isClassMethod:[self isClassMethod]];
  30. NSArray<NSString *> *methodComponents = [FLEXRuntimeUtility prettyArgumentComponentsForMethod:self.method];
  31. NSMutableArray<FLEXArgumentInputView *> *argumentInputViews = [NSMutableArray array];
  32. unsigned int argumentIndex = kFLEXNumberOfImplicitArgs;
  33. for (NSString *methodComponent in methodComponents) {
  34. char *argumentTypeEncoding = method_copyArgumentType(self.method, argumentIndex);
  35. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:argumentTypeEncoding];
  36. free(argumentTypeEncoding);
  37. inputView.backgroundColor = self.view.backgroundColor;
  38. inputView.title = methodComponent;
  39. [argumentInputViews addObject:inputView];
  40. argumentIndex++;
  41. }
  42. self.fieldEditorView.argumentInputViews = argumentInputViews;
  43. }
  44. - (BOOL)isClassMethod
  45. {
  46. return self.target && self.target == [self.target class];
  47. }
  48. - (NSString *)titleForActionButton
  49. {
  50. return @"Call";
  51. }
  52. - (void)actionButtonPressed:(id)sender
  53. {
  54. [super actionButtonPressed:sender];
  55. NSMutableArray *arguments = [NSMutableArray array];
  56. for (FLEXArgumentInputView *inputView in self.fieldEditorView.argumentInputViews) {
  57. id argumentValue = inputView.inputValue;
  58. if (!argumentValue) {
  59. // Use NSNulls as placeholders in the array. They will be interpreted as nil arguments.
  60. argumentValue = [NSNull null];
  61. }
  62. [arguments addObject:argumentValue];
  63. }
  64. NSError *error = nil;
  65. id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:&error];
  66. if (error) {
  67. NSString *title = @"Method Call Failed";
  68. NSString *message = [error localizedDescription];
  69. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  70. [alert show];
  71. } else {
  72. [self exploreObjectOrPopViewController:returnedObject];
  73. }
  74. }
  75. @end