ios 多继承问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 May 12, 2015 4417 views
    This topic created in 4007 days ago, the information mentioned may be changed or developed.

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

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

    提倡

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

    例如:

    @interface XXXBaseViewController : UIViewController

    @interface XXXBaseTableViewController : XXXBaseViewController <UITableViewDataSource, UITableViewDelegate>

    @interface BaseCollectionViewController : BaseViewController <UICollectionViewDataSource, UICollectionViewDelegate>

    后面两个都是自己实现的,之所以不用系统的是因为我发现系统的如 UITableViewController 有时候会有问题,例如某些下拉刷新库用不了,约束不能改,反正很多限制,不如自己实现。
    chengchao0311
        14
    chengchao0311  
       May 15, 2015
    runtime, class_addMethod, Method Swizzling.
    xi_lin
        15
    xi_lin  
       May 20, 2015
    补一个category
    About     Help     Advertise     Blog     API     FAQ     Solana     2485 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 07:56 PVG 15:56 LAX 00:56 JFK 03:56
    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