[心得] 找目前顯示的UIViewController
因為自己案子的需求,
需要在UIAlertView被dismiss的時候,
知道目前顯示的UIViewController是哪個。
找了些資料,無法透過UIKit提供的方法找到,
於是自己寫了一個UIViewController的category「TopViewController」。
分享於gist:
https://gist.github.com/1721691
1. 透過遞迴的方式找到最上層的UIViewController
2. 遞迴判斷的順序 a)目前的UIViewController是否有presentedViewController?
是 -> 由presentedViewController繼續遞迴
b)是否包含於UINavigationController裡?
是 -> 回傳UINavigationController的visibleViewController
c)是否本身就是UINavigationController?
是 -> 回傳visibleViewController
d)以上皆非 -> 回傳self
目前在自己案子都還算順利,
但不確定這樣的方法有沒有問題:)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.67.167.60
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章