Iphone 缺少代理阻止调用将使应用程序崩溃。在使用@protocol时,是否可以声明类似于@optional的块?

Iphone 缺少代理阻止调用将使应用程序崩溃。在使用@protocol时,是否可以声明类似于@optional的块?,iphone,xcode,delegates,objective-c-blocks,Iphone,Xcode,Delegates,Objective C Blocks,我正在为可拖动对象编写类 当平移完成时,我想使用块来委托它。 它工作得很好,但是在使用@protocol时,我应该如何设置像@optional这样的内容呢? 因为如果我不调用myObject.DiEndPanning应用程序崩溃 // MyClass.h #import <Foundation/Foundation.h> typedef void (^DelegateBlock)(void); @interface AHDraggableObject : UIView - (

我正在为可拖动对象编写类

当平移完成时,我想使用块来委托它。 它工作得很好,但是在使用@protocol时,我应该如何设置像@optional这样的内容呢? 因为如果我不调用myObject.DiEndPanning应用程序崩溃

// 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();
    }
}

您可以检查块是否已设置:

if (sender.state == UIGestureRecognizerStateEnded && _didEndPanning)
{
    _didEndPanning();
}

您可以检查块是否已设置:

if (sender.state == UIGestureRecognizerStateEnded && _didEndPanning)
{
    _didEndPanning();
}