V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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
tsinghan
V2EX  ›  iDev

ios 多继承问题

  •  
  •   tsinghan · 2015-05-12 16:39:25 +08:00 · 3542 次点击
    这是一个创建于 3243 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想给所有的controller都继承一个基类, 但是呢,我这个些controller 有的都已经继承了 UIViewController, 像这种情况怎么办? 用协议?

    15 条回复    2015-05-20 23:19:57 +08:00
    cxe2v
        1
    cxe2v  
       2015-05-12 16:46:00 +08:00
    让你的基类继承UIViewController,controller再继承基类
    googolxi
        2
    googolxi  
       2015-05-12 16:56:29 +08:00
    在代码中写一个BaseViewController,封装一些变量和方法进去,后边创建的控制器都继承BaseController,代码如下
    #import "BaseViewController.h"
    @interface NewsDetailViewController : BaseViewController
    likid
        3
    likid  
       2015-05-12 17:17:13 +08:00
    replace all
    rayshen
        4
    rayshen  
       2015-05-12 18:57:36 +08:00 via iPhone
    多继承即一个子类可以有多个父类,它继承了多个父类的特性。
    monsabre1
        5
    monsabre1  
       2015-05-12 19:09:32 +08:00
    不提倡
    这个是c++让人晕菜的原因之一
    tsinghan
        6
    tsinghan  
    OP
       2015-05-12 22:03:25 +08:00
    @cxe2v
    @googolxi
    但是如果有的controller继承 uiviewcontroller 有的继承UITableViewController 这种情况呢
    monsabre1
        7
    monsabre1  
       2015-05-12 22:22:51 +08:00
    objectivec

    提倡

    1.单一继承
    2.组合
    finian
        8
    finian  
       2015-05-12 22:26:20 +08:00
    @tsinghan 这种情况下只能用协议了。其实你需要解决什么问题?是这些 controller 类都需要有某些相同行为是么?
    tsinghan
        9
    tsinghan  
    OP
       2015-05-13 09:03:30 +08:00
    @finian 比如说,我想在所有的controller里面都实现一个判断网络状态的方法,我就想到定义一个基类,在这个基类里面去实现, 但是呢 我现在的controller有的继承UIViewController 有的继承UITableViewController, 是不是应该把继承UITableViewController的controller改成继承UIViewController,然后再实现tableview的协议, 这样就能按照
    @cxe2v
    @googolxi
    的说法 实现继承了
    cxe2v
        10
    cxe2v  
       2015-05-13 09:13:36 +08:00
    @tsinghan 如果实在不行你可以把判断网络状态的方法独立出来,所有controller去调用这个方法就可以了
    finian
        11
    finian  
       2015-05-13 09:41:01 +08:00
    @tsinghan 如楼上所说,将判断网络状态的方法独立出来,用组合而非继承的方式比较好
    tsinghan
        12
    tsinghan  
    OP
       2015-05-13 14:02:14 +08:00
    @cxe2v
    @finian 好的 多谢
    vincentxue
        13
    vincentxue  
       2015-05-13 14:05:48 +08:00
    我的工程一般有一个基类,两个子基类。

    例如:

    @interface XXXBaseViewController : UIViewController

    @interface XXXBaseTableViewController : XXXBaseViewController <UITableViewDataSource, UITableViewDelegate>

    @interface BaseCollectionViewController : BaseViewController <UICollectionViewDataSource, UICollectionViewDelegate>

    后面两个都是自己实现的,之所以不用系统的是因为我发现系统的如 UITableViewController 有时候会有问题,例如某些下拉刷新库用不了,约束不能改,反正很多限制,不如自己实现。
    chengchao0311
        14
    chengchao0311  
       2015-05-15 07:33:22 +08:00
    runtime, class_addMethod, Method Swizzling.
    xi_lin
        15
    xi_lin  
       2015-05-20 23:19:57 +08:00
    补一个category
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5219 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 07:22 · PVG 15:22 · LAX 00:22 · JFK 03:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.