12

We have received a HUGE project from outsourcing that we are trying to "repair". There are hundreds of view controllers within the project. Our goal is to easily determine which class we are currently looking at on the device.

Our solution (which didn't work, hence the SO question) follows.

Override the viewDidAppear method of UIViewController via a category with this:

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

This category would be put in the .pch of the project.

This would require no extra code to be put in the hundreds of View Controllers and be easily turned on and off. It didn't work because, as we've learned now, <meme>one does not simply override an existing method via category</meme>.

What are we missing?!?

adamweeks
  • 1,332
  • 2
  • 14
  • 21
  • This code has an infinite loop, doesn't it? You should call [super viewDidAppear:animated]; – Bruno Domingues Dec 13 '12 at 17:32
  • @BrunoDomingues since this is a category on UIViewController, calling super would call viewDidAppear on NSObject (UIViewController's superclass) which doesn't exist. – adamweeks Dec 13 '12 at 17:34
  • You could find current visible view (view controller) from UIWindow rootViewController. – 9dan Dec 13 '12 at 17:38
  • @9dan where exactly would that go? – adamweeks Dec 13 '12 at 17:41
  • I tried it here and with [self viewDidAppear:animated]; it really has an infinite loop, and with super crashes. In your project viewControllers' viewDidAppear does something? Because by default it does nothing. You can remove the line [self viewDidAppear:animated]; and it will work. – Bruno Domingues Dec 13 '12 at 17:44
  • I not very familiar with iOS (only Cocoa), but all visible views are must be added to a super view that is eventually contained in a window. So you can track down which are visible from the top most view (the view of rootViewController). Starts with rootViewController.view.subviews and look up all subviews of 'subview's, check out hidden, frame, and so on. – 9dan Dec 13 '12 at 17:49

7 Answers7

14

The answer is to swizzle 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
adamweeks
  • 1,332
  • 2
  • 14
  • 21
  • Been using this today and there's one "gotcha" that we've come across. If the subclass calls viewDidAppear but doesn't call [super viewDidAppear], you will not get a log. – adamweeks Dec 13 '12 at 22:06
  • Just came across this. For the sake anyone else reading comments, all UIViewController subclasses MUST call [super viewDidAppear], so if that's missing, that's a different programmer error. – Shinigami Feb 09 '16 at 05:31
7

viewWillAppear log

Here is a solution to print 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 this expression: expr -- (void) printf(" %s\n", (char *)object_getClassName($arg1))
  • check Automatically continue after evaluating actions.

enter image description here

This has helped me a lot whenever I got lost in the project!

deinit log

You can also add a log to see when your view controllers deinit is called:

  • Create another Symbolic Breakpoint
  • for Symbol, add -[UIViewController dealloc]
  • for Action, add a Debugger Command and this expression: expr -- (void) printf(" %s\n", (char *)object_getClassName($arg1))
  • check Automatically continue after evaluating actions.

enter image description here

This one is very handy to make sure the view controller gets released from memory and also a good indicator for catching the retain cycles.

Note: I wouldn't suggest using swizzling as it might risk your code being less maintainable.

Alchi
  • 799
  • 9
  • 19
3

Here is solution for this

In your .pch file include this

#define UIViewController MyViewController
#import "MyViewController.h"

Create your new UIViewController sub class as

.h file

#import <UIKit/UIKit.h>

#ifdef UIViewController
#undef UIViewController
#endif
@interface MyViewController : UIViewController

@end
#ifndef UIViewController
#define UIViewController MyViewController
#endif

And .m file

#import "MyViewController.h"

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
}

@end
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
1

Do the view controllers share a common base class? if so you could just put it there in the base class' implementation of [viewDidAppear:]. If they do not share a common base, then perhaps that would be a worthwhile task as it could be useful anyways going forwards (common analytics code, etc.)

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • Unfortunately not. They all are simply subclasses of UIViewController. Looking into method swizzling for the solution. – adamweeks Dec 13 '12 at 17:32
  • I know you've got hundreds of controllers, but a good find/replace could easily help you inherit from a common controller class. That would make this particular task easy, and have other benefits. – Joel Martinez Dec 13 '12 at 17:49
0

You can do a application wide find and replace from Xcode, but it won't necessarily find every case (but neither would the approaches that you tried). You could look for "[super viewDidLoad];" and replace with "[super viewDidLoad]; NSLog(@"Current View Class: %@", NSStringFromClass(self.class));"

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Looking for a simpler solution that would work application wide, rather than having to modify every class as described in the question. – adamweeks Dec 13 '12 at 18:06
0

Does the app use Navigation controllers to display the View Controllers? If so, you can use the NavigationController's 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);
    }
Mike M
  • 4,358
  • 1
  • 28
  • 48
0

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

Patrick Haaser
  • 353
  • 1
  • 3
  • 10