target action 分类: UI 2014-09-19 16:02 11人阅读 评论(0) 收藏

//为当前视图指定当视图接收到响应事件之后,由target来通过action方法进行响应.

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    //用实例变量存储外界传入的参数,方便在其他方法中使用
    _target = target;
    _action = action;
    _controlEvents = controlEvents;
    NSLog(@"%@",_target);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(UIControlEventTouchDown == _controlEvents)
    {
    //当当前视图接收到触摸事件之后,交由target去处理.
    [_target performSelector:_action withObject:self];
        
    }
    
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (UIControlEventTouchUpInside == _controlEvents) {
        [_target performSelector:_action withObject:self];
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(action)