Register the class name of all UIViewControllers in the project

We got the HUGE project from outsourcing, which we are trying to β€œrestore”. There are hundreds of controllers in the project. Our goal is to easily determine which class we are currently viewing on the device.

Our solution (which did not work, therefore, the SO question) follows.

Override the viewDidAppear UIViewController method through the category using this:

-(void)viewDidAppear:(BOOL)animated { NSLog(@"Current View Class: %@", NSStringFromClass(self.class)); [self viewDidAppear:animated]; //Also tried this: //[super viewDidAppear:animated]; } 

This category will be placed in the .pch project.

This does not require the addition of additional code to hundreds of view controllers and is easy to turn on and off. This did not work because, as we now know, meme , you are not just redefining the existing method through the / meme category.

What are we missing?!?

+9
ios objective-c uiviewcontroller nslog
source share
7 answers

The answer is to customize the methods! Here is what we came up with:

 #import "UIViewController+Logging.h" #import <objc/runtime.h> @implementation UIViewController (Logging) -(void)swizzled_viewDidAppear:(BOOL)animated { NSLog(@"Current View Class: %@", NSStringFromClass(self.class)); [self swizzled_viewDidAppear:animated]; } + (void)load { Method original, swizzled; original = class_getInstanceMethod(self, @selector(viewDidAppear:)); swizzled = class_getInstanceMethod(self, @selector(swizzled_viewDidAppear:)); method_exchangeImplementations(original, swizzled); } @end 
+14
source share

Here is the solution for this

Include this in your .pch file

 #define UIViewController MyViewController #import "MyViewController.h" 

Create a new subclass of the UIViewController class as

.h file

 #import <UIKit/UIKit.h> #ifdef UIViewController #undef UIViewController #endif @interface MyViewController : UIViewController @end #ifndef UIViewController #define UIViewController MyViewController #endif 

And the .m file

 #import "MyViewController.h" @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Current View Class: %@", NSStringFromClass(self.class)); } @end 
+3
source share

Here is the solution for printing the current view controller class name when it appears in the console:

  • Create a symbolic breakpoint in Xcode
  • for symbol add -[UIViewController viewWillAppear:]
  • for action, add a debugger command and the following expression:
  • expr -- (void) printf("πŸ”˜ %s\n", (char *)object_getClassName($arg1))
  • check the Automatically continue after evaluating actions box.

enter image description here

It helped me a lot when I got lost in the project!

+2
source share

Do view controllers have a common base class? if so, you can simply put it in the base class implementation [viewDidAppear:]. If they do not share a common base, then perhaps this would be a worthwhile task, since it can be useful in any case ahead (general analytics code, etc.).

+1
source share

You can find the application and replace it with Xcode, but it will not necessarily find every case (but also the approaches that you tried). You can search for "[super viewDidLoad]"; and replace it with "[super viewDidLoad]; NSLog (@" Class of the current view:% @ ", NSStringFromClass (self.class));

0
source share

Does the app use navigation controllers to display View controllers? If so, you can use the NavigationController methods to report the current controller:

  - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [self reportNewController:viewController]; } - (void) reportNewController:(UIViewController *)viewController { NSString *name = viewController.title; NSLog(@"Name is %@",name); } 
0
source share

You can use the swizzling method. Here is a good guide: http://nshipster.com/method-swizzling/

0
source share

All Articles