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 · 5062 次点击
    这是一个创建于 3263 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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   ·   我们的愿景   ·   实用小工具   ·   5196 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:14 · PVG 15:14 · LAX 00:14 · JFK 03:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.