vikeyToy 2013-04-08 07:29 采纳率: 0%
浏览 2061
已采纳

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 2013-04-08 09:16
    关注

    检测block是否设置:

    if (sender.state == UIGestureRecognizerStateEnded && _didEndPanning)
    {
        _didEndPanning();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波