ios 多继承问题 - V2EX
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 4245 次点击
    这是一个创建于 3880 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想给所有的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     Solana     3073 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 308ms UTC 12:00 PVG 20:00 LAX 04:00 JFK 07:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86