这是从IQKeyBoardManager里面看到的两个函数,觉得卧槽。太有用啦。跟大家分享一下:
//获取最上层的控制器:window最上层的控制器:包含UINavigationController
- (UIViewController*)topMostController
{
UIViewController *topController = [self rootViewController];
// Getting topMost ViewController
while ([topController presentedViewController]) topController = [topController presentedViewController];
// Returning topMost ViewController
return topController;
}
//当最上层的控制器是UINavigationController时。你需要通过该方法来获取UINavigationController的子控制器栈的最上层的UIViewController
- (UIViewController*)currentViewController;
{
UIViewController *currentViewController = [self topMostController];
while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
currentViewController = [(UINavigationController*)currentViewController topViewController];
return currentViewController;
}