在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为止。

IOS开发中响应者链的更多相关文章

  1. iOS开发中设置UITextField的占位文字的颜色,和光标的颜色

    在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...

  2. IOS开发中UI编写方式——code vs. xib vs.StoryBoard

    最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...

  3. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  4. 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...

  5. iOS 开发中常见的设计模式

    最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...

  6. ios开发中关闭textview控件的虚拟键盘

    在ios开发中,textfield控件在点击的时候出现虚拟键盘,关掉虚拟键盘可以通过虚拟键盘中的done button和点击view中的任意地方来关闭虚拟键盘. 1.第一种方法是textfield控件 ...

  7. iOS 开发之使用链式编程思想实现简单的计算器

    链式编程思想是将多个操作(多行代码)通过点号(.)链接在一起成为一句代码,使代码可读性好.例如 a(1).b(2).c(3). 链式编程思想最为关键的是,方法的返回值是block,block必须返回对 ...

  8. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  9. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

随机推荐

  1. 8--UI 初步认识 简易计算器

    UI是App的根基:一个App应该是先有UI界面,然后在UI的基础上增加实用功能(2)UI相对简单易学:UI普遍是学习过程中最简单的一块,能快速拥有成就感和学习兴趣(3)UI至关重要:开发中的绝大部分 ...

  2. java 设计模式之单利模式以及代理模式(静态)

    1:单利模式: public class Singleton { private static Singleton uniqueInstance = null; private Singleton() ...

  3. CodeForces 525D Arthur and Walls

    广搜.看了官方题解才会的..... 定义2*2的小矩阵,有三个是点,一个是星,这样的小矩阵被称为元素块. 首先把所有元素块压入队列,每次取出对头,检查是否还是元素块,如果是 那么将那个*改为点,否则跳 ...

  4. 一个好用简单的布局空间EasyUI

    之前项目中都是前端来新写的页面,对于很多后台管理系统来说,新写页面其实比较麻烦. 最近看到一款还是不错的开源页面框架EasyUi http://www.jeasyui.com/index.php 这是 ...

  5. access restriction

    一.既然存在访问规则,那么修改访问规则即可.打开项目的Build Path Configuration页面,打开报错的JAR包,选中Access rules条目,选择右侧的编辑按钮,添加一个访问规则即 ...

  6. C#入门经典-第15章ListBox,CheckedListBox

  7. 用FusionChartsFree做饼状图、柱状图、折线图的实例

    1.先来看看要进行表现的页面:myChart.jsp <%@ page language="java" contentType="text/html; charse ...

  8. drawRect & layoutSubviews 调用时间

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘.   layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSub ...

  9. 运行时设计(Design at Run-time)

    1.定义 传统软件开发必须经历“设计时”和“运行时”两个阶段,运行时设计,顾名思义,就是在软件运行过程中,对软件进行实时设计修改,而无需再次进行编译,用户即可使用. “运行时设计(Design at ...

  10. Eclipse开发Maven项目提示:程序包org.junit不存在解决方案

    原因: 个人考虑产生此错误的原因是因为Eclipse中对于测试和开发的鉴定不明?Intellij中没有错误,因为Intellij对项目的管理就是同Maven结构的. 解决方案: 原来的junit的sc ...