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.
我们的解决方案(无效,因此SO问题)如下。
Override the viewDidAppear method of UIViewController via a category with this:
通过以下类别覆盖UIViewController的viewDidAppear方法:
-(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.
此类别将放在项目的.pch中。
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?!?
我们缺少什么?!?
6 个解决方案
#1
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
#2
3
Here is solution for this
这是解决方案
In your .pch file include this
在.pch文件中包含此内容
#define UIViewController MyViewController
#import "MyViewController.h"
Create your new UIViewController sub class as
创建新的UIViewController子类为
.h file
#import <UIKit/UIKit.h>
#ifdef UIViewController
#undef UIViewController
#endif
@interface MyViewController : UIViewController
@end
#ifndef UIViewController
#define UIViewController MyViewController
#endif
And .m file
和.m文件
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
}
@end
#3
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.)
视图控制器是否共享一个公共基类?如果是这样,你可以把它放在[viewDidAppear:]的基类实现中。如果他们不共享一个共同的基础,那么也许这将是一项有价值的任务,因为它可能是有用的,无论如何前进(常见的分析代码等)
#4
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));"
您可以在Xcode中进行应用程序范围的查找和替换,但不一定能找到所有情况(但您尝试的方法也不一定)。你可以找“[super viewDidLoad];”并替换为“[super viewDidLoad]; NSLog(@”Current View Class:%@“,NSStringFromClass(self.class));”
#5
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:
该应用程序是否使用导航控制器来显示视图控制器?如果是这样,您可以使用NavigationController的方法报告当前控制器:
- (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);
}
#6
0
You can use method swizzling. Here is a nice guide: http://nshipster.com/method-swizzling/
你可以使用方法调配。这是一个很好的指南:http://nshipster.com/method-swizzling/
#1
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
#2
3
Here is solution for this
这是解决方案
In your .pch file include this
在.pch文件中包含此内容
#define UIViewController MyViewController
#import "MyViewController.h"
Create your new UIViewController sub class as
创建新的UIViewController子类为
.h file
#import <UIKit/UIKit.h>
#ifdef UIViewController
#undef UIViewController
#endif
@interface MyViewController : UIViewController
@end
#ifndef UIViewController
#define UIViewController MyViewController
#endif
And .m file
和.m文件
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Current View Class: %@", NSStringFromClass(self.class));
}
@end
#3
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.)
视图控制器是否共享一个公共基类?如果是这样,你可以把它放在[viewDidAppear:]的基类实现中。如果他们不共享一个共同的基础,那么也许这将是一项有价值的任务,因为它可能是有用的,无论如何前进(常见的分析代码等)
#4
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));"
您可以在Xcode中进行应用程序范围的查找和替换,但不一定能找到所有情况(但您尝试的方法也不一定)。你可以找“[super viewDidLoad];”并替换为“[super viewDidLoad]; NSLog(@”Current View Class:%@“,NSStringFromClass(self.class));”
#5
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:
该应用程序是否使用导航控制器来显示视图控制器?如果是这样,您可以使用NavigationController的方法报告当前控制器:
- (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);
}
#6
0
You can use method swizzling. Here is a nice guide: http://nshipster.com/method-swizzling/
你可以使用方法调配。这是一个很好的指南:http://nshipster.com/method-swizzling/