ios 中有没有获取当前 controller 的方法? - 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 中有没有获取当前 controller 的方法?

  •  
  •   tsinghan 2015-05-14 14:53:06 +08:00 5671 次点击
    这是一个创建于 3806 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ios 中有没有获取当前controller的方法?

    11 条回复    2015-05-22 14:18:12 +08:00
    yuan925
        1
    yuan925  
       2015-05-14 15:25:51 +08:00
    根据响应链模式,获取当前view的controller,可以用self.nextResponder获取
    ycge234
        2
    ycge234  
       2015-05-14 15:46:45 +08:00
    github上有个ios-category,常用的方法都有,包括这个。
    chmlai
        3
    chmlai  
       2015-05-14 15:57:18 +08:00
    什么叫当前 Controller?
    tsinghan
        4
    tsinghan  
    OP
       2015-05-14 16:32:53 +08:00
    @yuan925
    @ycge234
    @chmlai
    额 我直接通过 self.window?.rootViewController?.childViewControllers.last 这样拿到当前的controller,
    superleexpert
        5
    superleexpert  
       2015-05-14 17:23:40 +08:00
    可以设置个单例保存
    knightlhs
        6
    knightlhs  
       2015-05-14 17:26:31 +08:00
    当前 Controller 是个什么鬼?当前正在运行的 Controller 有一大堆……
    话说你的意思是获取当前主线程的 Controller?当前显示的 View 的 Controller?
    metrue
        7
    metrue  
       2015-05-15 09:57:31 +08:00 via iPhone
    当前controller?主进程所处的controller?
    yanchao7511461
        8
    yanchao7511461  
       2015-05-16 07:44:08 +08:00
    lz肯定是说当前函数所在的controller,比如函数中有一个需求判断当前controller,我猜是这样的
    xunfeng
        9
    xunfeng  
       2015-05-16 10:51:50 +08:00
    弄个全局变量,再controller的viewDid appear 方法中,更新当前可见的viewcontrollers (具体可以通过发送通知,或者全局变量直接更新(感觉不是很优雅)),这个方法可以获得当前程序中所有在“可见”的controller。不过貌似对于pop window就无法跟踪,可以看看文档viewDidappear在啥时候不会被调用。
    vincentxue
        10
    vincentxue  
       2015-05-16 14:01:04 +08:00
    完全不明白楼主的意思,什么叫当前的 Controller,不是 self 吗,为什么要获取?
    zhugeafanti
        11
    zhugeafanti  
       2015-05-22 14:18:12 +08:00
    - (UIViewController *)viewController {
    UIResponder *next = [self nextResponder];

    do {
    if ([next isKindOfClass:[UIViewController class]]) {
    return (UIViewController *)next;
    }

    next = [next nextResponder];
    } while (next != nil);

    return nil;
    }

    自己用的一个view的category
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1029 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 18:43 PVG 02:43 LAX 11:43 JFK 14:43
    Do have faith in what you're doing.
    ubao 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