首页 > 代码库 > target action

target action

//为当前视图指定当视图接收到响应事件之后,由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];
    }
}

target action