• <iOS小技巧> 返回上级目录操作Goback()方法


    Goback()方法功能:返回上一级界面,通过判断 popViewControllerAnimated 类型是否为空,来判断是present还是pop出来,然后直接做了releaseSelf操作;

    - (void)goBack
    {
       
    IDSLOG(@"self: %@, parent: %@", self, [self parentViewController]);
       
       
    id page = [self presentingViewController];
       
    IDSLOG(@"presenting page: %@", page);
       
        id vc = [self.navigationController popViewControllerAnimated:YES];
        IDSLOG(@"pop the = %@", vc);
        if (nil == vc) {
            [
    self dismissViewControllerAnimated:YES completion:^{
               
            }];
        }
       
        [
    self releaseSelf];
    }
     
    releaseSelf()方法功能:用来释放通知内存,和Goback()方法结合,以防忘记释放默认通知;

    - (void)releaseSelf
    {
       
    //sub class implements.
       
       
    IDSLOG(@"self: %@", self);
       
        [[
    NSNotificationCenter defaultCenter] removeObserver:self];
    }
     


    PS:在每个文件前面要加这两句释放内存的语句

    - (
    void)dealloc
    {
       
    IDSLOG(@"dealloc - IDSGameRoomHomePage");
    }


    - (
    void)releaseSelf
    {
        [
    super releaseSelf];
    }
     
    二、我的想法
     
     
    Goback这种方法,使用起来很便捷,又注意了内存泄漏,之前写的时候,每次都要对应Push 或者 present 来写返回操作,现在一个 [self goback] 就搞定了,我觉得这是一个比较便捷又不容易出问题的好方法。
     
    三、思考与行动
     
    1.Goback方法这样写会不会存在问题?如果有,是否思考过更好的解决办法?
     
    2.releaseSelf 和 dealloc 有啥区别?为什么有dealloc还需要releaseSelf方法?合成一个方法的弊端在哪里?


     
  • 相关阅读:
    vue, 同一个页面有多处地方需要上传图片
    单张图片上传,vue
    replace 替换只会替换找到的第一个字符
    vue ant design table中rowSelection属性的应用
    一般做页面时需要注意的事项
    vue 为form 表单赋值 获取form表单的值
    vue 父子组件中的传值
    vue 页面跳组件,实现点击浏览器自带返回箭头,返回到上一个页面,而不是返回道上个路由
    vue ant design a-table 的分页
    初建vuex项目
  • 原文地址:https://www.cnblogs.com/firstrate/p/7134516.html
Copyright © 2020-2023  润新知