2 vikeytoy vikeyToy 于 2013.04.08 15:29 提问

iphone关于delegates崩溃

给一个可拖拽的对象写了类。

在快完成的时候想要用delegates使用block,运行正常,不知道能不能在使用@protocol时有类似 @optional设置?如果不调用myObject.didEndPanning,应用就会崩溃。

// MyClass.h

#import <Foundation/Foundation.h>

typedef void (^DelegateBlock)(void);

@interface AHDraggableObject : UIView

- (id)initDragableView:(UIView *)view inView:(UIView *)parentView withBorderOffset:(int)offset;

@property (nonatomic, strong) UIView *holder;
@property (nonatomic, weak) DelegateBlock didEndPanning;

@end

// MyClass.m

- (void)handlePanning:(UIPanGestureRecognizer *)sender
{
    CGPoint translation = [sender translationInView:_parentView];
    sender.view.center = CGPointMake(sender.view.center.x + translation.x, sender.view.center.y + translation.y);
    [sender setTranslation:CGPointMake(0, 0) inView:_parentView];

    [self checkBondaries:sender withOffset:_offset inView:_parentView];

    //  end of pan - invoke event
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        _didEndPanning();
    }
}

调用

//ViewController.m
-(void)viewDidLoad {
    _drag = [[MRShape alloc] initDragableView:_testImageView inView:self.view withBorderOffset:40];
    _drag.didEndPanning = ^
    {
     // if didEndPanning call is missing, app crash !
    };
}

1个回答

ce_la_vie
ce_la_vie   2013.04.08 17:16
已采纳

检测block是否设置:

if (sender.state == UIGestureRecognizerStateEnded && _didEndPanning)
{
    _didEndPanning();
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!