IOS app最小化之后 打开刷新界面

业务场景

今天和朋友在群里讨论,他说需要在app最小化回来之后 刷新当前页面,也就是再次网络请求。

方案

  • 1 每个界面注册通知,收到通知掉起网络,然后在appdelegate 最小化时候发送通知。
  • 2 还是用通知,写一个基类,所有controller继承refreshNewWork方法
  • 3 没想到...😆

第一种方法当我们页面特别多得时候 显然有点不合理了,目前看起来第二种方案好像还可以。

首先看baseviewController方法

 [self recieveDefaultcenter];
//通知
- (void)recieveDefaultcenter{
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    
    [notiCenter addObserver:self selector:@selector(receiveNotification:) name:@"refreshNetWork" object:nil];

}
- (void)receiveNotification:(NSNotification *)notify{
    
    [self getCurrentViewController];
    
}
- (void)getCurrentViewController{
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    NSLog(@"当前controller%@",[currentVC class]);
    NSString *className = [NSString stringWithFormat:@"%@",[currentVC class]];
    
    ViewControllerConst *viewConst = [ViewControllerConst shareViewConst];
    NSArray *arr = [viewConst registViewControllers];
    for (int i = 0; i<arr.count; i++) {
        
        if ([className isEqualToString:arr[i]]) {
            
            viewConst.isRefresh = YES;
        }
    }
    
    [self refreshNewWork:className isRefresh:viewConst.isRefresh];

}
-(void)refreshNewWork:(NSString *)className isRefresh:(BOOL)isRefresh{



}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

viewconst 是我建的一个类

4B46630E-39A3-4CD8-9D4E-AC8B1EC6F07A.png

想通过这个类 去过滤获取到的viewcontroller

本来想这样搞

    ViewControllerConst *viewConst = [ViewControllerConst shareViewConst];
    NSArray *arr = [viewConst registViewControllers];
    for (int i = 0; i<arr.count; i++) {
        
        if ([className isEqualToString:arr[i]]) {
            
            viewConst.isRefresh = YES;
        }
    }
    
    [self refreshNewWork:className isRefresh:viewConst.isRefresh];

这个过滤 有问题 永远会是yes 所以卡在这了

然后我在子类里面过滤了

-(void)refreshNewWork:(NSString *)className isRefresh:(BOOL)isRefresh{
 
    NSString *currenClass = [NSString stringWithFormat:@"%@",[self class]];
    if ([currenClass isEqualToString:className]) {
        
        NSLog(@"首页刷新");
    }
    
}

为什么要判定,如果不判定 当我点击第二个界面最小化之后 第一个界面 还是会走刷新方法。

想知道各位有没有什么好的方法实现.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,066评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,061评论 19 139
  • 昨天晚上我做了一个很奇怪的梦,我梦到我住在一个医院里面,我的老师是一位年长男性,他也住在医院里面。我们住在一栋楼房...
    百合儿阅读 1,468评论 0 0
  • 磁盘缓存:比喻为每天都要用钱,而如果每次都去银行取钱,会很不方便。但是带上个几百块钱在身上,就会很方便。 地址映射...
    我是一个好人吗阅读 1,706评论 0 0
  • 在夜里,天很阴沉 看见你躲着月亮 我躲着你,把自己 交给心虚的机缘 忐忑不安,高大的黑影 将你挡在镜子里面 四目相...
    ChocOne阅读 1,747评论 0 0

友情链接更多精彩内容