Wie @Sausage Modulation Matrix sagt, dies ist View-Controller-Kategorie, die Ihnen hilft. new_viewWillAppear:
oder new_viewWillDisappear:
Aufruf häufig, wenn UIViewController
Lebenszyklus-Methode aufrufen. In dieser Swizzle-Methode können Sie AppDelegate-Methoden gemäß Ihren Anforderungen ausführen.
UIViewController + ViewControllerSwizzle.m
#import "UIViewController+ViewControllerSwizzle.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
void standard_swizzle(Class cls, SEL original, SEL replacement) {
Method originalMethod;
if ((originalMethod = class_getClassMethod(cls, original)))
{
Method replacementMethod = class_getClassMethod(cls, replacement);
method_exchangeImplementations(originalMethod, replacementMethod);
}
else
{
IMP replacementImplementation = method_setImplementation(class_getInstanceMethod(cls, replacement), class_getMethodImplementation(cls, original));
if (!class_addMethod(cls, original, replacementImplementation, method_getTypeEncoding(class_getInstanceMethod(cls, replacement)))) method_setImplementation(class_getInstanceMethod(cls, original), replacementImplementation);
}
}
@implementation UIViewController (ViewControllerSwizzle)
+(void)load
{
if (self == [UIViewController class]) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *methods = @[@"viewWillAppear:", @"viewWillDisappear:"];
for (NSString* methodName in methods) {
standard_swizzle(self, NSSelectorFromString(methodName), NSSelectorFromString([NSString stringWithFormat:@"new_%@", methodName]));
}
});
}
}
-(void)new_viewWillAppear:(BOOL)animated
{
[self new_viewWillAppear:animated];
[[AppDelegate sharedManager] viewWillAppearStuff]; //AppDelegate custom method.
}
-(void)new_viewWillDisappear:(BOOL)animated
{
[self new_viewWillDisappear:animated];
[[AppDelegate sharedManager] viewWillDisppearStuff]; //AppDelegate custom method.
}
@end
Hier ist einfach AppDelegate stuff:
AppDelegate.h Datei:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
+(AppDelegate *)sharedManager;
-(void)viewWillAppearStuff;
-(void)viewWillDisppearStuff;
@end
AppDelegate.m Datei:
#import "AppDelegate.h"
@implementation AppDelegate
+(AppDelegate *)sharedManager
{
static AppDelegate *sharedMyManager = nil;
@synchronized(self)
{
if (sharedMyManager == nil)
{
sharedMyManager = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
}
return sharedMyManager;
}
-(void)viewWillAppearStuff
{
/*
Do Something.
*/
}
-(void)viewWillDisppearStuff
{
/*
Do Something.
*/
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
@end
Glückliche Kodierung.
Sie sollten Ihre Frage mit spezifischeren Details darüber, was Sie tatsächlich erreichen möchten, aktualisieren. – rmaddy