[英]Missing delegate block call will crash the app. Is it possible to declare block like @optional when using @protocol?
I'm writing class for dragable object. 我正在为可拖动对象编写类。
When panning is finished I want to delegate it using blocks. 平移完成后,我想使用块将其委派。 It's working fine, but how should I set something like @optional when using @protocol?
它工作正常,但是使用@protocol时应该如何设置类似@optional的内容? Because if I don't call myObject.didEndPanning app crash.
因为如果我不调用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();
}
}
Call 呼叫
//ViewController.m
-(void)viewDidLoad {
_drag = [[MRShape alloc] initDragableView:_testImageView inView:self.view withBorderOffset:40];
_drag.didEndPanning = ^
{
// if didEndPanning call is missing, app crash !
};
}
You can check whether the block has been set: 您可以检查是否已设置块:
if (sender.state == UIGestureRecognizerStateEnded && _didEndPanning)
{
_didEndPanning();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.