• [功能]点击ImageView进入页面,时间响应者链实现


      app点击一个按钮跳转到另外一个控制器非常常用,但是如果是点击的是UIImageView如何通过模态视图进入另外一个控制器呢?万一这个UIImageView在自定义的cell或者view里面,那该如何做呢?

      首先要先知道的是时间响应者链,

    响应者链处理原则

    1. 点击检测视图或者第一响应者传递事件或动作消息给它的视图控制器(如果它有的话);如果没有一个视图控制器,就传递给它的父视图。

    2. 如果一个视图或者它的视图控制器不能处理这个事件或动作消息,它将传递给该视图的父视图。

    3. 在这个视图层次中的每个后续的父视图遵循上述的模式,如果它不能处理这个事件或动作消息的话。

    4. 最顶层的视图如果不能处理这个事件或动作消息,就传递给UIWindow对象来处理。

    5. 如果UIWindow 对象不能处理,就传给单件应用程序对象UIApplication。

    如果应用程序对象也不能处理这个事件或动作消息,将抛弃它。

    首先我们新建一个事件响应者链的类别,添加一个方法到uiview

    #import <UIKit/UIKit.h>
    
    @interface UIView (Additions)
    
    -(UIViewController *)viewController;  //寻找控制器的方法
    
    @end

    实现方法

    #import "UIView+Additions.h"
    
    @implementation UIView (Additions)
    
    
    -(UIViewController *)viewController{
        
        UIResponder *next = [self nextResponder];  //拿到下一个响应者
        
        do {
            if ([next isKindOfClass:[UIViewController class]]) { //如果下一个响应者是UIViewController
                
                return (UIViewController *)next;
            
            }else{
                next = [next nextResponder];
            }
        } while (next != nil);
        
        return  nil;
    }
    
    
    @end

    这样我们就通过类别的方法通过事件响应者链找到控制器的View

    接下来自定义一UIImageView,头文件是DDImageView.h

    #import <UIKit/UIKit.h>
    
    typedef void(^ImageBlock)(void);   //通过block实现
    
    @interface DDImageView : UIImageView
    
    @property(nonatomic,copy)ImageBlock touchBlock;
    @end

    实现方法

    #import "DDImageView.h"
    
    @implementation DDImageView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.userInteractionEnabled = YES; //这里很重要,一定要YES
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchAction:)];//给每一个UIImageView添加一个手势
            [self addGestureRecognizer:tapGesture];
            [tapGesture release];
        }
        return self;
    }
    
    -(void)touchAction:(UITapGestureRecognizer *)tapGesture{
        
        if (self.touchBlock) {
            _touchBlock();   //通过Block回调实现
        }
        
    }
    
    -(void)dealloc{
        
        [super dealloc];
        Block_release(_touchBlock);
    }

    接着自定义一个Cell,里面存放我们自定义的view

    #import <UIKit/UIKit.h>
    
    
    @class DataModel;
    @class DDImageView;
    @interface ImageViewCell : UITableViewCell{
        
        DDImageView     *_userImage;            //用户头像
    }
    
    
    //数据模型对象
    @property(nonatomic,retain)DataModel *dataModel;

    实现方法,这里复写了Model的set方法

    -(void)setDataModel:(DataModel *)dataModel{
        
        if (_dataModel != dataModel) {
            [_dataModel release];
            _dataModel = [dataModel retain];
        }
        
        __block ImageViewCell *this = self;
        _userImage.touchBlock = ^{
            NSString *name = this.imageViewModel.user.screen_name;
            UserViewController *userViewController = [[UserViewController alloc] init];
            userViewController.userName = name;
            [this.viewController.navigationController pushViewController:userViewController animated:YES];
            [userViewController release]; 
            
        };
    }

    这样就实现了回调功能,能够通过Block实现通过点击头像进入另外一个控制器

  • 相关阅读:
    堆排序算法(C#实现)
    在 .NET 2.0 中享受 .NET 3.0的特性
    火花:使用文件管理功能管理我的连接
    我们可以做的更好
    有价值的文章
    网上掏宝
    WPF绑定技术一步步学
    结构类型需要重载GetHashCode和Equals
    关于扩展Visual Studio 2005的相关资料
    插件模型应该考虑的问题
  • 原文地址:https://www.cnblogs.com/iOS-dd/p/3450069.html
Copyright © 2020-2023  润新知