![](/img/trans.png)
[英]MFMessageComposeViewController - how can I find out when this has been fully presented?
[英]How to find out when the method of an object has been called?
我有一個UIViewController
對象。 我想知道何時調用了viewDidLoad
方法。
我知道我可以在子類中重寫此方法並找出何時調用它。
還有什么要知道何時調用viewDidLoad
方法嗎?
編輯-
我有幾個視圖控制器被推到UINavigationController
。 其中一些視圖控制器隱藏導航欄。 可以使用此處給出的答案來完成。
override func viewWillAppear(animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: animated)
super.viewWillAppear(animated)
}
override func viewWillDisappear(animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: animated)
super.viewWillDisappear(animated)
}
目前,我已經在許多視圖控制器中重復了此代碼。
我正在嘗試刪除重復的代碼。
一種實現方法是創建一個包含該代碼的超類,並使其視圖控制器隱藏該超類的導航欄子類。 但是,一些隱藏導航欄的視圖控制器從UITableViewController
和UICollectionViewController
。
我正在嘗試創建一個類,該類將在視圖控制器上調用viewWillAppear(_:)
和viewWillDisappear(_:)
進行觀察,然后調用該方法來隱藏和顯示導航欄。
您可以嘗試通過Swizzling在忠實的Objective-C中實現目標。
輸入您的Xcode:
創建一個新文件-> Objective-C,文件名為“ Lifecycle”,文件類型為“ Category”,類為“ UIViewController”
如果不存在,Xcode將提供創建橋接頭文件的功能。
放入您的橋接頭新創建的文件中: #import "UIViewController+Lifecycle.h"
您的UIViewController+Lifecycle.h
應該如下所示:
#import <UIKit/UIKit.h>
@interface UIViewController (Lifecycle)
+ (void)setViewControllerSwizzlingActive:(BOOL)wantedState;
@end
UIViewController+Lifecycle.m
應該如下所示: #import <objc/runtime.h>
#import "UIViewController+Lifecycle.h"
@implementation UIViewController (Lifecycle)
+ (void)setViewControllerSwizzlingActive:(BOOL)wantedState
{
static BOOL isWorkaroundActive = NO;
if (isWorkaroundActive != wantedState)
{
[self exchangeViewWillAppearMethod];
[self exchangeViewWillDisappearMethod];
isWorkaroundActive = wantedState;
}
}
+ (void)exchangeViewWillAppearMethod
{
Method original = class_getInstanceMethod(self, @selector(viewWillAppear:));
Method fake = class_getInstanceMethod(self, @selector(fakeViewWillAppear:));
method_exchangeImplementations(original, fake);
}
+ (void)exchangeViewWillDisappearMethod
{
Method original = class_getInstanceMethod(self, @selector(viewWillDisappear:));
Method fake = class_getInstanceMethod(self, @selector(fakeViewWillDisappear:));
method_exchangeImplementations(original, fake);
}
- (void)fakeViewWillAppear:(BOOL *)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
return [self fakeViewWillAppear:animated];
}
- (void)fakeViewWillDisappear:(BOOL *)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
return [self fakeViewWillDisappear:animated];
}
@end
AppDelegate's
didFinishLaunching方法中說: ViewController.setViewControllerSwizzlingActive(true)
如果您想要Swift版本-這將是您的任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.