HINSPHeapStackDetailTableViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // RMHeapStackDetailTableViewController.m
  3. // HeapInspectorExample
  4. //
  5. // Created by Christian Menschel on 29.08.14.
  6. // Copyright (c) 2014 tapwork. All rights reserved.
  7. //
  8. #import "HINSPHeapStackDetailTableViewController.h"
  9. #import "HINSPResponderChainViewController.h"
  10. #import "HINSPShowViewController.h"
  11. #import "HINSPTableViewCell.h"
  12. #import "HINSPClassDumpTableViewController.h"
  13. #import "HINSPRefHistoryTableViewController.h"
  14. #import "NSObject+HeapInspector.h"
  15. static NSString *const kCellTitleShow = @"Show";
  16. static NSString *const kCellTitleResponderChain = @"Responder Chain";
  17. static NSString *const kCellTitleMethods = @"Methods";
  18. static NSString *const kCellTitleReferenceHistory = @"Reference History";
  19. static NSString *const kCellTitleIvars = @"iVars";
  20. static NSString *const kCellTitleProperties = @"Properties";
  21. static NSString *const kCellTitleRecursiveDesc = @"Recursive Description";
  22. static const CGFloat kHeaderViewHeight = 100.0f;
  23. @interface HINSPHeapStackDetailTableViewController ()
  24. @end
  25. @implementation HINSPHeapStackDetailTableViewController
  26. {
  27. UITextView *_headerTextView;
  28. UIFont *_boldFont;
  29. UIFont *_regFont;
  30. }
  31. #pragma mark - View Life Cycle
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. self.title = @"Object Inspector";
  36. _boldFont = [UIFont boldSystemFontOfSize:12];
  37. _regFont = [UIFont systemFontOfSize:12];
  38. [self setupHeaderView];
  39. [self prepareDataSource];
  40. }
  41. - (void)setupHeaderView
  42. {
  43. CGRect headerViewFrame = CGRectMake(0.0, 0.0,
  44. self.tableView.bounds.size.width,
  45. kHeaderViewHeight);
  46. UIView *headerView = [[UIView alloc] initWithFrame:headerViewFrame];
  47. headerView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  48. CGRect titleLabelFrame = CGRectMake(5.0, 5.0,
  49. headerView.bounds.size.width - 10.0,
  50. 15.0);
  51. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleLabelFrame];
  52. CGRect textViewFrame = CGRectMake(0.0, CGRectGetMaxY(titleLabel.frame),
  53. headerView.bounds.size.width,
  54. headerView.bounds.size.height - CGRectGetMaxY(titleLabel.frame));
  55. UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame];
  56. titleLabel.text = [NSString stringWithFormat:@"%s: %p",
  57. object_getClassName(self.inspectingObject),
  58. self.inspectingObject];
  59. titleLabel.font = [UIFont boldSystemFontOfSize:12];
  60. titleLabel.backgroundColor = [UIColor clearColor];
  61. [headerView addSubview:titleLabel];
  62. // Add a switch if the object is of bool type
  63. if ([self.inspectingObject respondsToSelector:@selector(boolValue)] &&
  64. [NSStringFromClass([self.inspectingObject class]) isEqualToString:@"__NSCFBoolean"]) {
  65. UISwitch *boolSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
  66. CGRect switchFrame = boolSwitch.frame;
  67. switchFrame.origin.x = headerViewFrame.size.width - switchFrame.size.width - 10.0;
  68. switchFrame.origin.y = 5.0;
  69. boolSwitch.frame = switchFrame;
  70. [boolSwitch addTarget:self action:@selector(boolSwitchToggle:) forControlEvents:UIControlEventValueChanged];
  71. boolSwitch.on = [self.inspectingObject boolValue];
  72. [headerView addSubview:boolSwitch];
  73. }
  74. textView.editable = NO;
  75. textView.backgroundColor = [UIColor clearColor];
  76. _headerTextView = textView;
  77. [headerView addSubview:textView];
  78. textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  79. headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  80. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  81. self.tableView.tableHeaderView = headerView;
  82. [self updateHeaderView];
  83. }
  84. - (void)updateHeaderView
  85. {
  86. if ([self.inspectingObject conformsToProtocol:@protocol(NSObject)] &&
  87. [self.inspectingObject respondsToSelector:@selector(description)]) {
  88. NSInteger retainCount = CFGetRetainCount((__bridge CFTypeRef)self.inspectingObject);
  89. NSString *retainCountString = [NSString stringWithFormat:@"Retain count: %ld\n", (long)retainCount];
  90. NSString *infoHeaderText = [retainCountString stringByAppendingString:[self.inspectingObject description]];
  91. _headerTextView.text = infoHeaderText;
  92. }
  93. }
  94. #pragma mark - Actions
  95. - (void)boolSwitchToggle:(UISwitch *)boolSwitch
  96. {
  97. self.inspectingObject = @(boolSwitch.on);
  98. [self updateHeaderView];
  99. }
  100. #pragma mark - DataSource
  101. - (void)prepareDataSource
  102. {
  103. NSMutableArray *dataSource = [@[] mutableCopy];
  104. NSArray *refHistory = [NSObject referenceHistoryForObject:self.inspectingObject];
  105. if ([refHistory count]) {
  106. [dataSource addObject:kCellTitleReferenceHistory];
  107. }
  108. if ([self.inspectingObject isKindOfClass:[UIResponder class]]) {
  109. [dataSource addObject:kCellTitleResponderChain];
  110. }
  111. if ([self.inspectingObject isKindOfClass:[UIView class]] ||
  112. [self.inspectingObject isKindOfClass:[UIImage class]] ||
  113. [self.inspectingObject isKindOfClass:[UIViewController class]]) {
  114. [dataSource addObject:kCellTitleShow];
  115. if ([self.inspectingObject isKindOfClass:[UIView class]]) {
  116. [dataSource addObject:kCellTitleRecursiveDesc];
  117. }
  118. }
  119. [dataSource addObject:kCellTitleMethods];
  120. [dataSource addObject:kCellTitleProperties];
  121. [dataSource addObject:kCellTitleIvars];
  122. self.dataSource = [dataSource copy];
  123. [self.tableView reloadData];
  124. }
  125. #pragma mark - UITableview dataSource & Delegate
  126. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellIdent
  129. forIndexPath:indexPath];
  130. NSString *item = self.dataSource[indexPath.row];
  131. cell.textLabel.text = item;
  132. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  133. if ([item isEqualToString:kCellTitleReferenceHistory]) {
  134. cell.textLabel.font = _boldFont;
  135. } else {
  136. cell.textLabel.font = _regFont;
  137. }
  138. return cell;
  139. }
  140. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. UIViewController *targetController = nil;
  143. NSString *item = self.dataSource[indexPath.row];
  144. if ([item isEqualToString:kCellTitleResponderChain]) {
  145. targetController = [[HINSPResponderChainViewController alloc] initWithObject:self.inspectingObject];
  146. } else if ([item isEqualToString:kCellTitleShow]) {
  147. targetController = [[HINSPShowViewController alloc] initWithObject:self.inspectingObject];
  148. } else if ([item isEqualToString:kCellTitleMethods]) {
  149. targetController = [[HINSPClassDumpTableViewController alloc] initWithObject:self.inspectingObject type:RMClassDumpMethods];
  150. } else if ([item isEqualToString:kCellTitleIvars]) {
  151. targetController = [[HINSPClassDumpTableViewController alloc] initWithObject:self.inspectingObject type:RMClassDumpIvar];
  152. } else if ([item isEqualToString:kCellTitleProperties]) {
  153. targetController = [[HINSPClassDumpTableViewController alloc] initWithObject:self.inspectingObject type:RMClassDumpProperties];
  154. } else if ([item isEqualToString:kCellTitleRecursiveDesc]) {
  155. #pragma clang diagnostic push
  156. #pragma clang diagnostic ignored "-Wundeclared-selector"
  157. NSString *recursiveDesc = [self.inspectingObject performSelector:@selector(recursiveDescription)];
  158. targetController = [[HINSPShowViewController alloc] initWithObject:recursiveDesc];
  159. ((HINSPShowViewController *)targetController).shouldShowEditButton = NO;
  160. } else if ([item isEqualToString:kCellTitleReferenceHistory]) {
  161. targetController = [[HINSPRefHistoryTableViewController alloc] initWithObject:self.inspectingObject];
  162. }
  163. #pragma clang diagnostic pop
  164. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  165. [self.navigationController pushViewController:targetController animated:YES];
  166. }
  167. @end