FLEXMethodCallingViewController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. NSError *error = nil;
  67. id returnedObject = [FLEXRuntimeUtility performSelector:method_getName(self.method) onObject:self.target withArguments:arguments error:&error];
  68. if (error) {
  69. NSString *title = @"Method Call Failed";
  70. NSString *message = [error localizedDescription];
  71. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  72. [alert show];
  73. } else if (returnedObject) {
  74. // For non-nil (or void) return types, push an explorer view controller to display the returned object
  75. FLEXObjectExplorerViewController *explorerViewController = [FLEXObjectExplorerFactory explorerViewControllerForObject:returnedObject];
  76. [self.navigationController pushViewController:explorerViewController animated:YES];
  77. } else {
  78. // If we didn't get a returned object but the method call succeeded,
  79. // pop this view controller off the stack to indicate that the call went through.
  80. [self.navigationController popViewControllerAnimated:YES];
  81. }
  82. }
  83. @end