selector的使用

SEL类型相当于C++中的函数指针。
@selector:(SEL) 也就是说@selector :后可以接收SEL类型的函数指针。

就我个人来看,selector就好比是一个非常灵活的函数指针。但是它要比C语言中的函数指针更加灵活,因为它的值并不是某个函数的地址,而仅仅是方法名。这一点比C++中的指向类成员指针要灵活得多,尽管它只是对方法(操作)的引用而无法引用数据成员(属性)。
不过,概念上,IMP类型才是一个方法指针类型。
默认的API函数,只能接受一个方向的滑动操作,因此使用selector来进行多个滑动操作  

//---right swipe (default)---
    UISwipeGestureRecognizer *swipeRihgtGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self addGestureRecognizer:swipeRihgtGesture];
    [swipeRihgtGesture release];
    
    //---left swipe---
    UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
    swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:swipeLeftGesture];
    [swipeLeftGesture release];

-(IBAction) handleSwipeGesture:(UIGestureRecognizer *) sender {
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];   
    switch (direction) {
        case UISwipeGestureRecognizerDirectionRight:
            [self movePrevMonth];
            break;
        case UISwipeGestureRecognizerDirectionLeft:            
            [self moveNextMonth];
            break;
        default:
            break;
    }        
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值