ios 自定义cell类中获取当前controller push

有时候在自定义cell的过程中,当cell中又button的时候,把button的点击时间写在cell中的时候,需要获取到cell的父视图控制器然后push,可以自建一个类,命名为:

GetCurrentNaviGationController,在.h文件 中提供一个类方法

+(UIViewController *)viewController:(UIView *)view;

 

然后在.m中实现

+(UIViewController *)viewController:(UIView *)view{

    /// Finds the view's view controller.

    // Traverse responder chain. Return first found view controller, which will be the view's view controller.

    UIResponder *responder = view;

    while ((responder = [responder nextResponder]))

        if ([responder isKindOfClass: [UIViewController class]])

            return (UIViewController *)responder;

    // If the view controller isn't found, return nil.

    return nil;

}

 

然后在想调用的cell中引入头文件,然后调用

[[GetCurrentNaviGationController viewController:self].navigationController pushViewController:controller animated:YES];

posted @ 2015-10-29 11:09  damnbird  阅读(575)  评论(0编辑  收藏  举报