- (UIViewController *)getCurrentVC
{
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
return currentVC;
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]){
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
currentVC = rootVC;
}
return currentVC;
}
func getCurrentVC()->UIViewController{
let rootViewController = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController
let currentVC = self.getCurrentVCFrom(rootVC: rootViewController!)
return currentVC
}
fileprivate func getCurrentVCFrom(rootVC:UIViewController)->UIViewController{
var currentVC:UIViewController = rootVC;
var rootVC2 = rootVC
if rootVC.presentedViewController != nil{
rootVC2 = rootVC2.presentedViewController!
}
if (rootVC2.isKind(of: UITabBarController.classForCoder())){
rootVC2 = self.getCurrentVCFrom(rootVC: (rootVC2 as! UITabBarController).selectedViewController!)
}else if rootVC2.isKind(of: UINavigationController.classForCoder()){
currentVC = self.getCurrentVCFrom(rootVC: (rootVC2 as! UINavigationController).visibleViewController!)
}else{
currentVC = rootVC2
}
return currentVC
}