iOS 开发实用技术导航
NSHipster 中文版
› http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
› http://www.cocos2d-iphone.org/
CocoaPods
› http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
› http://code.google.com/mobile/analytics/
WWDC
› https://developer.apple.com/wwdc/
Design Guides and Resources
› https://developer.apple.com/design/
Transcripts of WWDC sessions
› http://asciiwwdc.com
Cocoa with Love
› http://cocoawithlove.com/
Cocoa Dev Central
› http://cocoadevcentral.com/
NSHipster
› http://nshipster.com/
Style Guides
› Google Objective-C Style Guide
› NYTimes Objective-C Style Guide
Useful Tools and Services
› Charles Web Debugging Proxy
› Smore
davidlau
V2EX  ›  iDev

当 Controller 膨胀至上千行,混杂着业务逻辑和 UI 渲染逻辑时,如何重构?

  •  
  •   davidlau ·
    liuslevis · Sep 18, 2015 · 5360 views
    This topic created in 4063 days ago, the information mentioned may be changed or developed.
    用什么设计模式 /思路?
    23 replies  •  2015-09-22 11:20:03 +08:00
    zhangchioulin
    zhangchioulin
    Sep 18, 2015❤️1
    1
    一步一步来呗,先不创建更多的文件,先把方法抽出,一个方法做一件事情
    然后抽取模型
    然后再根据方法抽出 View
    view 还能重构成,设置这个 view 控件的位置的一个 model 和生成 view 控件一个 Model
    ikaros
    ikaros
    Sep 18, 2015
    2
    你题目已经回答了,把混杂的东西抽出来呗,单独写一个渲染的函数, 业务逻辑 抽出来做成 service 由 controller 调用
    ipconfiger
    ipconfiger
    Sep 18, 2015
    3
    MVVM 啊
    StargazerWikiv
    StargazerWikiv
    Sep 18, 2015
    4
    当初是怎么想的呢? 1k 多代码一个 controller 。。。
    shoaly
    shoaly
    Sep 18, 2015
    5
    说白了 controller 就是一个文件夹, 这个文件夹下面现在太多东西了 , 先试着把文件分类, 然后建立更多的 controller , 将这些文件放过去.
    iEason
    iEason
    Sep 18, 2015
    6
    数据层的全扔到 model 里去,视图该封装的封装。
    MVVM 也是不错的选择
    xi_lin
    xi_lin
    Sep 18, 2015
    7
    页面元素特别多,事件绑定和响应处理特别多怎么破?大家的 View 处理 event 吗?我对 view 的理解一直是纯渲染,交互都给 controller 处理。然后在写复杂页面的时候 controller 就膨胀起来了。我能想到的只有分 category 处理。
    pi1ot
    pi1ot
    Sep 18, 2015❤️1
    8
    辞职走人,下个接手的倒霉蛋会重写的
    zhigang1992
    zhigang1992
    Sep 18, 2015
    9
    MVVM + Rx, iOS 的 DesignPatten 太多了,各种 delegate, KVO, target action, notification, blocks 等等等等。还是 Rx 来的舒服,代码好读好管理
    newtonisaac
    newtonisaac
    Sep 18, 2015
    10
    辞职走人,下个接手的倒霉蛋会重写的
    yangjie6020
    yangjie6020
    Sep 18, 2015
    11
    抽象 封装 剥离
    chairuosen
    chairuosen
    Sep 18, 2015
    12
    @xi_lin 安利一下 vue
    chairuosen
    chairuosen
    Sep 18, 2015
    13
    @xi_lin sorry 看错节点了哈哈哈哈
    xiaonengshou
    xiaonengshou
    Sep 18, 2015
    14
    angular?
    xi_lin
    xi_lin
    Sep 18, 2015
    15
    @zhigang1992 Rx 要放弃 7.0 可是公司还要支持,好难过。。
    xi_lin
    xi_lin
    Sep 18, 2015
    16
    gsanidt
    gsanidt
    Sep 18, 2015
    17
    推荐你看看这篇文章: https://www.objc.io/issues/1-view-controllers/
    damngood
    damngood
    Sep 18, 2015
    18
    1. mvvm
    2. containment
    3. category || extension
    4. 常规功能封装
    cxq
    cxq
    Sep 18, 2015
    19
    MVVM 用起来很舒服

    不过最近看别人的 android 项目代码,
    感觉他 Activity 数量和代码都非常少,看起来非常整洁
    主要是因为他们有 Fragment , 和 ListView 的 Adapter 独立出来了
    ibcker
    ibcker
    Sep 20, 2015
    20
    mvvm
    ibcker
    ibcker
    Sep 20, 2015
    21
    @cxq ListView 的 Adapter 在 iOS 同样适用,我写 tableview 一般就把 datasource 独立出来。
    cxq
    cxq
    Sep 20, 2015
    22
    @ibcker 恩,我也是, 一发现变大了 就重构抽出来。另外最近还开始躲用自定义 View 抽出不少代码来。但是因为还在用 objective-c 所以看起来文件还是比他们多很多。
    Seakey
    Seakey
    Sep 22, 2015
    23
    @xi_lin 建议将 Controller 分为业务逻辑控制器和视图控制器
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1330 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 20:10 · PVG 04:10 · LAX 12:10 · JFK 15:10
    ♥ Do have faith in what you're doing.