IOS开发中响应者链
在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开发中响应者链的更多相关文章
- iOS开发中设置UITextField的占位文字的颜色,和光标的颜色
在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...
- IOS开发中UI编写方式——code vs. xib vs.StoryBoard
最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- 解析iOS开发中的FirstResponder第一响应对象
1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...
- iOS 开发中常见的设计模式
最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...
- ios开发中关闭textview控件的虚拟键盘
在ios开发中,textfield控件在点击的时候出现虚拟键盘,关掉虚拟键盘可以通过虚拟键盘中的done button和点击view中的任意地方来关闭虚拟键盘. 1.第一种方法是textfield控件 ...
- iOS 开发之使用链式编程思想实现简单的计算器
链式编程思想是将多个操作(多行代码)通过点号(.)链接在一起成为一句代码,使代码可读性好.例如 a(1).b(2).c(3). 链式编程思想最为关键的是,方法的返回值是block,block必须返回对 ...
- 总结iOS开发中的断点续传那些事儿
前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...
- iOS开发中静态库之".framework静态库"的制作及使用篇
iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...
随机推荐
- Git学习 -- 搭建Git服务器
搭建环境 服务器端:CentOS 6.5 IP:192.168.101.129 客户端:CentOS 6.5 . Windows 服务器端: 创建repository版本库,例如/srv/test ...
- java导读
导读: 我们学习Java大概有3个方向,第一,桌面系统,包括C/S结构:第二,J2ME,面向无限领域,很有潜力的家伙,看看中国的手机用户就知道了.第三,面向企业应用.计算的平台,J2EE. ...
- Chrome 43+浏览器 Cookies encrypted_value解密脚本
python 3.3.5 # -*- coding: utf-8 -*- # Used information from: # http://stackoverflow.com/questions/4 ...
- hdu_5719_Arrange(脑洞题)
题目连接:hdu_5719_Arrange 题意: 给你1-n这 n个数,设一个排列的第i个数为Ai, Bi为A1到Ai的最小值,Ci为C1到Ci的最大值,问你有多少种排列方式,然后输出取模后的答案 ...
- redis五种数据类型的使用
redis五种数据类型的使用 redis五种数据类型的使用 (摘自:http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml ) 1.S ...
- sublime使用方法
一.sublime菜单简介[常用功能及快捷键] [Edit菜单] 1.line行操作快捷键 ctrl+] 增加缩进 ctrl+[ 减小缩进 ctrl+shift+D 复制当前行 ctrl+shift+ ...
- hql 链接查询
第一部分.连接查询 一.内连接 内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值.内连接分三种: 1.等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询 ...
- 洛谷U4727 小L 的二叉树
U4727 小L 的二叉树 题目背景 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣. 所以,小L当时卡在了二叉树. 题目描述 在计算机科学中,二叉树 ...
- A Game
A Game 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi and Little Ho are playing a game. There is a ...
- HDU 5512 Pagodas
2015 ACM / ICPC 沈阳现场赛 D 题 找了一小时规律......发现是个GCD. #include<cstdio> #include<cstring> #incl ...