UIResponder - 3
2016-01-08 17:49
567 查看
如果想通过视图获取自身,或者是superView的视图控制器类,想起来觉得非常不可以思议,但是通过UIResponder的响应链就非常好实现,我们可以重新看回UIResponder - 1 文章,有一张响应者链的图:
![](https://img-blog.csdn.net/20160108173959342?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
通过这个,就非常好实现了,参考:
http://apluck.iteye.com/blog/1748432
- (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;
}
测试:
1.直接添加子类:
#import "LBAccessViewController.h"
@interface LBAccessViewController ()
@end
@implementation LBAccessViewController
- (void)viewDidLoad
{
[super viewDidLoad];
LBAccessView* myview = [[LBAccessView alloc] init];
[self.view addSubview:myview];
NSLog(@"self : %@", self);
NSLog(@"access : %@", [myview viewController]);
}
@end
#pragma mark - LBAccessView.
@implementation LBAccessView
- (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;
}
@end
输出:
![](https://img-blog.csdn.net/20160108174234751?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
2.先添加其他视图,即嵌套一层视图:
UIView* containView = [[UIView alloc] init];
[self.view addSubview:containView];
LBAccessView* myview = [[LBAccessView alloc] init];
[containView addSubview:myview];
NSLog(@"self : %@", self);
NSLog(@"access : %@", [myview viewController]);
输出依旧
当然还有别的方法:
如果存在导航栏NavigationController可以使用这个方法:
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.
拿到当前看得到的视图控制器,如果木有的话,返回最顶层的视图控制器。
参考:
Reference - 1 : http://apluck.iteye.com/blog/1748432
Reference - 2 : http://www.cnblogs.com/maxfong/p/3423633.html
通过这个,就非常好实现了,参考:
http://apluck.iteye.com/blog/1748432
- (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;
}
测试:
1.直接添加子类:
#import "LBAccessViewController.h"
@interface LBAccessViewController ()
@end
@implementation LBAccessViewController
- (void)viewDidLoad
{
[super viewDidLoad];
LBAccessView* myview = [[LBAccessView alloc] init];
[self.view addSubview:myview];
NSLog(@"self : %@", self);
NSLog(@"access : %@", [myview viewController]);
}
@end
#pragma mark - LBAccessView.
@implementation LBAccessView
- (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;
}
@end
输出:
2.先添加其他视图,即嵌套一层视图:
UIView* containView = [[UIView alloc] init];
[self.view addSubview:containView];
LBAccessView* myview = [[LBAccessView alloc] init];
[containView addSubview:myview];
NSLog(@"self : %@", self);
NSLog(@"access : %@", [myview viewController]);
输出依旧
当然还有别的方法:
如果存在导航栏NavigationController可以使用这个方法:
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.
拿到当前看得到的视图控制器,如果木有的话,返回最顶层的视图控制器。
参考:
Reference - 1 : http://apluck.iteye.com/blog/1748432
Reference - 2 : http://www.cnblogs.com/maxfong/p/3423633.html
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器