前言
不知道大家有沒有遇到過在做輪播圖的時候,有點輪播圖展示的是廣告,有的是活動,等等還有其他的,當前點擊某個輪播的時候要跳轉到不同的控制器,點擊事件是在控制器寫的,為了避免控制器代碼過多,顯示的臃腫。我創建了一個UIWindow的分類,暫且叫Model (GetCurrentVC)
實現方法
谷歌還有很多方法,下面這個方法親測有效,有需要的可以參考借鑒。
一:
@interfaceUIWindow (GetCurrentVC)- (UIViewController*)getCurrentVC;@end
二:
#import"UIWindow+GetCurrentVC.h"@implementationUIWindow (GetCurrentVC)- (UIViewController*)getCurrentVC {UIViewController*result =nil;UIWindow* window = [[UIApplicationsharedApplication]keyWindow];if(window.windowLevel!=UIWindowLevelNormal){NSArray*windows = [[UIApplicationsharedApplication]windows];for(UIWindow* tmpWininwindows){if(tmpWin.windowLevel==UIWindowLevelNormal){window = tmpWin;break;}}}UIView*frontView = [[windowsubviews]objectAtIndex:0];idnextResponder = [frontViewnextResponder];if([nextResponderisKindOfClass:[UIViewControllerclass]])result = nextResponder;elseresult = window.rootViewController;returnresult;}@end
總結
以上就是iOS如何獲取當前View所在控制器的實現方法,希望本文對大家開發iOS能有一定的幫助,如有有疑問大家可以留言交流。
新聞熱點
疑難解答