我知道只要特定的视图控制器出现并消失,就会调用viewWillAppear
和viewWillDisappear
方法。
我希望只要应用程序中的任何视图控制器出现或消失,就会调用AppDelegate
方法。
这可能吗?
答案 0 :(得分:0)
而不是覆盖UIViewController(它要求所有视图控制器都必须从它继承,从而引入紧耦合),而不是为UIViewController创建扩展,并调用viewWillAppear和viewWillDisappear。
此链接包含在Swift中调用UIViewController所需的所有详细信息和代码
http://nshipster.com/method-swizzling/
它提供了swizzle viewWillAppear的代码。您可以在xxx_viewWillAppear方法中调用您的app delegate方法。
答案 1 :(得分:0)
正如@Sausage调制矩阵所说,这是视图控制器类别,它可以帮助您。在new_viewWillAppear:
生命周期方法调用时,new_viewWillDisappear:
或UIViewController
通常会调用。
在这个混合方法中,您可以根据自己的要求执行appDelegate方法。
#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
以下是简单的AppDelegate内容:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
+(AppDelegate *)sharedManager;
-(void)viewWillAppearStuff;
-(void)viewWillDisppearStuff;
@end
#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
快乐编码。
答案 2 :(得分:0)
由于无法从Swift 3.1及更高版本调用Objective-C类方法,因此将需要另一种方法。我这样做是为了使viewWillDisappear消失以阻止微调器旋转。
extension UIViewController {
@objc func viewWillDisappearOverride(_ animated: Bool) {
self.viewWillDisappearOverride(animated) //Incase we need to override this method
if self.isMovingFromParent {
SpinnerManager.shared.stopSpinner()
}
}
static func swizzleViewWillDisappear() {
//Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
if self != UIViewController.self {
return
}
let _: () = {
let originalSelector = #selector(UIViewController.viewWillDisappear(_:))
let swizzledSelector = #selector(UIViewController.viewWillDisappearOverride(_:))
guard let originalMethod = class_getInstanceMethod(self, originalSelector),
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}()
}
}
然后要激活它,我只是从UIViewController.swizzleViewWillDisappear()
内部的AppDelegate调用了didFinishLaunchingWithOptions
答案 3 :(得分:-1)
首先,将包含此功能的子类UIViewController。所有视图控制器都将继承此新类。其次,根据需要在appDelegate中编写您的公共方法。第三,按照以下方式制作自定义方法:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let appDelegate = UIApplication.sharedApplication().delegate as? AppNameAppDelegate{
appDelegate.doCustomMethod()
}
}