寫在前面
在我們操作頁面跳轉時,如果目前的類不是UIViewcontroller(下面用VC表示),你會不會寫一個代理,或者block給VC傳遞資訊,然後在VC裡面進行
///假如targetVc是将要跳轉的頁面
[self.navigationController pushViewController:targetVc animated:YES];
拿tableViewCell做例子,如果每個頁面展示的tableViewCell中,如果存在不少的這樣操作,就會寫很多代理或者block,如果不這樣,那又該怎麼做呢,思路是擷取目前顯示在最頂層的VC,網上有很多方法,先看一個普通的方法
- (UIViewController *)currentViewController {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
UIViewController *vc = keyWindow.rootViewController;
while (vc.presentedViewController) {
vc = vc.presentedViewController;
if ([vc isKindOfClass:[UINavigationController class]]) {
vc = [(UINavigationController *)vc visibleViewController];
} else if ([vc isKindOfClass:[UITabBarController class]]) {
vc = [(UITabBarController *)vc selectedViewController];
}
}
return vc;
}
上面的方法可以或許可以擷取最頂層的VC(我在網上找的一個,沒有測試,這裡隻做對比),但是有沒有感覺很繁瑣,當然也可以把它寫在一個工具裡面,每次用這個工具裡面的這個方法,也可以擷取,但是我始終覺得繁瑣,哈哈;
利用Runtime實作
runtime是一個好玩的東西,上一篇我簡單說了它的一些常用功能,這裡結合Category來實作擷取目前VC需求
1.建立一個基于UIApplication的分類
點選下一步就建好了
2.利用Runtime添加屬性
在UIApplication+CurrentViewController.h檔案中,添加
///用于擷取目前 UIViewController
@property (nonatomic, weak) UIViewController *currentViewController;
在UIApplication+CurrentViewController.m檔案中,引入頭檔案
#import <objc/runtime.h>
利用runtime實作屬性的get set方法
///set
- (void)setCurrentViewController:(UIViewController *)currentViewController{
objc_setAssociatedObject(self, @selector(currentViewController), currentViewController, OBJC_ASSOCIATION_ASSIGN);
}
///get
- (UIViewController *)currentViewController{
return objc_getAssociatedObject(self, _cmd);
}
3.實作
在需要擷取目前VC的檔案中,引入頭檔案,也可以直接将該頭檔案放入宏檔案中
#import "UIApplication+CurrentViewController.h"
在VC的viewWillAppear方法中,添加
[UIApplication sharedApplication].currentViewController = self;
這樣,我們在任何一個地方擷取,隻需要添加如下代碼,就可以擷取目前的VC,擷取到之後,不管是push、present還是performSegueWithIdentifier,都可以實作頁面的跳轉
UIViewController *viewVc = [UIApplication sharedApplication].currentViewController;
4.延伸
上面在需要用到的在每一個VC中都需要添加
[UIApplication sharedApplication].currentViewController = self;
那麼,可以建一個基于UIViewController的基類BaseViewController,然後在BaseViewController的viewWillAppear方法中添加上述代碼,我們在建立VC時,隻需要繼承BaseViewController就可以了!
*****************Swift實作*****************
extension UIApplication {
struct RuntimecurrVcKey {
static let currVcKey = UnsafeRawPointer.init(bitPattern: "currVcKey".hashValue)
}
var currViewController: UIViewController? {
get {
return objc_getAssociatedObject(self, UIApplication.RuntimecurrVcKey.currVcKey!) as? UIViewController
}
set(newValue) {
objc_setAssociatedObject(self, UIApplication.RuntimecurrVcKey.currVcKey!, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
}