在IOS开发中,有时候会遇到如下情况:在页面1上有一个RedView,在RedView上有一个GreenView,在GreenView上有一个button,这些view的创建代码如下:
1、AppDelegate.m
//
// AppDelegate.m
// 响应者链
//
// Created by mac on 16/5/10.
// Copyright © 2016年 mzw. All rights reserved.
// #import "AppDelegate.h"
#import "RootViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor lightGrayColor];
[self.window makeKeyAndVisible]; RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = nav; return YES;
} @end
2、RootViewController.m
//
// RootViewController.m
// 响应者链
//
// Created by mac on 16/5/10.
// Copyright © 2016年 mzw. All rights reserved.
// #import "RootViewController.h"
#import "FirstVCViewController.h"
#import "RedView.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
RedView *redVC = [[RedView alloc]initWithFrame:CGRectMake(, , , )];
[self.view addSubview:redVC]; } @end
3、RedView.m
//
// RedView.m
// 响应者链
//
// Created by mac on 16/5/10.
// Copyright © 2016年 mzw. All rights reserved.
// #import "RedView.h"
#import "GreenView.h" @implementation RedView -(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor redColor];
GreenView *grennView = [[GreenView alloc]initWithFrame:CGRectMake( , , ,)];
[self addSubview:grennView]; }
return self;
} @end
4、GreenView.m
//
// GreenView.m
// 响应者链
//
// Created by mac on 16/5/10.
// Copyright © 2016年 mzw. All rights reserved.
// #import "GreenView.h"
#import "FirstVCViewController.h"
#import "UIView+ViewController.h" @implementation GreenView -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor greenColor];
UIButton *myBtn = [[UIButton alloc]initWithFrame:CGRectMake(, , , )];
myBtn.backgroundColor = [UIColor orangeColor];
[myBtn setTitle:@"导航按钮" forState:UIControlStateNormal];
[myBtn addTarget:self action:@selector(myBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myBtn];
} return self;
} -(void)myBtnAction :(UIButton*)sender{ FirstVCViewController *firstVC = [[FirstVCViewController alloc]init];
[self.viewController.navigationController pushViewController:firstVC animated:YES]; } @end
这时候我们想在GreenView.m中实现点击myBtn之后跳转到FirstVCViewController的实体firstVC,但发现其实我们写不了第34行,因为在myBtnAction方法中,self指的是GreenView的实例,而GreenView的实例greenView是没有.navigationController方法的,这就需要通过View的从属关系去找到grennView的响应者redView,然后找到redView的响应者RootVC。RootVC才有.navigationController的方法,这样就可以实现点击按钮跳转到另一个ViewController了。实现方式如下,给UIView使用Category扩展一个方法,方法名字叫viewController,也就是找一个UIView对象的所属的ViewController对象,viewController类别实现如下:
1、UIView+ViewController.h中:
//
// UIView+ViewController.h
// Project-WXWeibo26
//
// Created by keyzhang on 14-9-29.
// Copyright (c) 2014年 keyzhang. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (ViewController) - (UIViewController *)viewController; @end
2、UIView+ViewController.m中:
//
// UIView+ViewController.m
// Project-WXWeibo26
//
// Created by keyzhang on 14-9-29.
// Copyright (c) 2014年 keyzhang. All rights reserved.
// #import "UIView+ViewController.h" @implementation UIView (ViewController) - (UIViewController *)viewController
{
UIResponder *next = self.nextResponder;
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
} next = next.nextResponder;
} while (next != nil); return nil;
} @end
方法的核心就是去判断一个UIView对象的响应者所属的类是不是UIViewController类,如果不是的话就继续找它的响应者的响应者,直到找到响应者是UIViewController为止。