iOS 获取当前顶层的ViewController

#pragma mark - topVC

- (UIViewController *)theTopviewControler {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

使用方法

UIViewController *topmostVC = [self topViewController];

时间: 2024-10-11 02:20:13

iOS 获取当前顶层的ViewController的相关文章

iOS 获取UIView所在的VIewController

写程序的时候我们经常要封装代码,当你封装了一个UIView的子类里需要调用所在ViewController的方法用如下代码调取所在的VIewController #pragma mark - 获取所在ViewController - (UIViewController *)superViewController:(id)sender { for (UIView* next = [self superview]; next; next = next.superview) { UIResponder

iOS开发——获取当前屏幕显示的viewcontroller

获取当前屏幕显示的viewcontroller,然后想怎么跳就怎么跳. - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplic

iOS 获取当前正在显示的ViewController

//获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication shar

ios 获取当前view的controller

 //获取view的controller  - (UIViewController *)viewController {     for (UIView* next = [self superview]; next; next = next.superview) {         UIResponder *nextResponder = [next nextResponder];          if ([nextResponder isKindOfClass:[UIViewControll

iOS连续dismiss几个ViewController的方法

原文链接:http://blog.csdn.net/longshihua/article/details/51282388 presentViewController是经常会用到的展现ViewController的方式,而显示和去除presentViewController也是很简单的,主要是下面两个方法: - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag

检测SDWebImage有没有缓存图片 IOS 获取网络图片大小

NSURL *url = [NSURL URLWithString:[model.content objectForKey:@"image"]];             //请求网络地址数据的同步方法             //因为这个方法在子线程(全局队列)中执行,所以不需要考虑死线程的问题             SDWebImageManager *manager = [SDWebImageManager sharedManager];              [manag

iOS获取汉字的拼音

在iOS开发中经常涉及到汉字的排序,最常见的就是需要根据首字母的字符顺序排列,比如常见的通讯录等.总结出来,大致可以分为两种方法,其中参考文献[1]中提供的方法十分复杂,并且代码量很大,不建议采用.另一种方法是直接采用Core Foundation中提供的方法,十分简单,封装好的代码如下: 1 - (NSString *)transform:(NSString *)chinese{ 2 //将NSString装换成NSMutableString 3 NSMutableString *pinyin

iOS获取本地视频和网络URL视频的缩略图方法

iOS获取本地视频和网络URL视频的缩略图方法 字数222 阅读612 评论0 喜欢13 首先大家先添加AVFoundation和CoreMedia.framework两个框架 第一种本地视频获取缩略图 NSString *path = @"www.51ios.net/本地路径" MPMoviePlayerController *51iosMPMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileU

ios 获取屏幕的属性和宽度

1.app尺寸,去掉状态栏 CGRect r = [ UIScreen mainScreen ].applicationFrame; r=0,20,320,460 另外:self.view.bounds.size 2.屏幕尺寸 CGRect rx = [ UIScreen mainScreen ].bounds; r=0,0,320,480 3.状态栏尺寸 CGRect rect; rect = [[UIApplication sharedApplication] statusBarFrame]