前言
不知道大家有沒有遇到過在做輪播圖的時候,有點輪播圖展示的是廣告,有的是活動,等等還有其他的,當前點擊某個輪播的時候要跳轉到不同的控制器,點擊事件是在控制器寫的,為了避免控制器代碼過多,顯示的臃腫。我創建了一個UIWindow的分類,暫且叫Model (GetCurrentVC)
實現方法
谷歌還有很多方法,下面這個方法親測有效,有需要的可以參考借鑒。
一:
1
2
3
4
5
|
二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#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; else result = window.rootViewController; returnresult; } @end |
總結
以上就是iOS如何獲取當前View所在控制器的實現方法,希望本文對大家開發iOS能有一定的幫助,如有有疑問大家可以留言交流。
原文鏈接:http://www.jianshu.com/p/0c76eed32c2c