• 在view中实现UIViewController的跳转 By H.L


    view中是不能进行UIViewController的push,pop等操作的,若进行跳转操作,一般是用代理,block,通知等实现,那如何实现在ViewController的subView中实现跳转操作呢,其实只要获取该view所在的ViewController即可。

    获取view所在UIViewController

    UIView+UIViewController.h

    #import <UIKit/UIKit.h>
    
    @interface UIView (UIViewController)
    
    - (UIViewController *)viewController;
    
    @end
    

    UIView+UIViewController.m

    #import "UIView+UIViewController.h"
    
    @implementation UIView (UIViewController)
    
    - (UIViewController *)viewController {
        
        //通过响应者链,获得view所在的视图控制器
        UIResponder *next = self.nextResponder;
        do {
            //判断响应者对象是否是视图控制器类型
            if ([next isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)next;
            }
            next = next.nextResponder;
        }while(next != nil);
        
        return nil;
    }
    
    @end

    or

    - (UIViewController *)viewController {
        // Traverse responder chain. Return first found view controller, which will be the view's view controller.
        UIResponder *responder = self;
        while ((responder = [responder nextResponder]))
            if ([responder isKindOfClass: [UIViewController class]])
                return (UIViewController *)responder;
        
        return nil;
    }
    - (UIViewController*)viewController {
        for (UIView* next = [self superview]; next; next = next.superview) {
            UIResponder* nextResponder = [next nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                return (UIViewController*)nextResponder;
            }
        }
        return nil;
    }
    

    其它 iOS 获取当前正在显示的ViewController 

      

    使用:

    在subview中导入  #import "UIView+UIViewController.h"

    -(void)viewDidSelect
    {
        WebViewController *vc = [[WebViewController alloc] init];
        vc.webUrl =@"http://www.baidu.com";
        
        //如果VC 是添加在Nav 上的 用
        [self.viewController.navigationController pushViewController:ViewController animated:YES];
        
        //else 如果没有添加用
        [self presentModalViewController: ViewController animated:YES];
    }
  • 相关阅读:
    从面向对象到SOA
    我对国内软件开发类书籍出版与写作的体会与努力
    MSDN for 2010的那些麻烦事
    金旭亮新作《.NET 4.0面向对象编程漫谈》之序“穿越梦想、起锚远航”
    Silverlight应用程序的本地通讯
    C#中Dictionary的用法
    泛型
    动态规划算法
    C# Timer
    面向对象程序设计寒假作业1
  • 原文地址:https://www.cnblogs.com/sixindev/p/4856879.html
Copyright © 2020-2023  润新知