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 · 4934 views
    This topic created in 4194 days ago, the information mentioned may be changed or developed.

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

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

    提倡

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

    例如:

    @interface XXXBaseViewController : UIViewController

    @interface XXXBaseTableViewController : XXXBaseViewController <UITableViewDataSource, UITableViewDelegate>

    @interface BaseCollectionViewController : BaseViewController <UICollectionViewDataSource, UICollectionViewDelegate>

    后面两个都是自己实现的,之所以不用系统的是因为我发现系统的如 UITableViewController 有时候会有问题,例如某些下拉刷新库用不了,约束不能改,反正很多限制,不如自己实现。
    chengchao0311
    chengchao0311
    May 15, 2015
    14
    runtime, class_addMethod, Method Swizzling.
    xi_lin
    xi_lin
    May 20, 2015
    15
    补一个category
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3653 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 05:20 · PVG 13:20 · LAX 21:20 · JFK 00:20
    ♥ Do have faith in what you're doing.