使用类别时崩溃

时间:2014-11-03 14:51:24

标签: ios objective-c uitouch

我创建了一个UITouch类类,我的代码是:

 - (id)initInView:(UIView *)view;
{
CGRect frame = view.frame;    
CGPoint centerPoint = CGPointMake(frame.size.width * 0.5f, frame.size.height * 0.5f);
return [self initAtPoint:centerPoint inView:view];
}

- (id)initAtPoint:(CGPoint)point inWindow:(UIWindow *)window;
{
 self = [super init];
if (self == nil) {
    return nil;
}

// Create a fake tap touch
_tapCount = 1;
_locationInWindow = point;
_previousLocationInWindow = _locationInWindow;

UIView *hitTestView = [window hitTest:_locationInWindow withEvent:nil];

_window = [window retain];
_view = [hitTestView retain];
if ([self respondsToSelector:@selector(setGestureView:)]) {
    [self setGestureView:hitTestView];
}
_phase = UITouchPhaseBegan;
_touchFlags._firstTouchForView = 1;
_touchFlags._isTap = 1;
_timestamp = [[NSProcessInfo processInfo] systemUptime];

return self;
}

- (id)initAtPoint:(CGPoint)point inView:(UIView *)view;
{
 return [self initAtPoint:[view.window convertPoint:point fromView:view] inWindow:view.window];
}

- (void)setPhase:(UITouchPhase)phase;
{
_phase = phase;
_timestamp = [[NSProcessInfo processInfo] systemUptime];
}

但是当我调用它时我得到了这个崩溃 - [UITouch initAtPoint:inView:]:发送到实例的无法识别的选择器 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您说您创建了一个类别,但未包含您的类别的定义。

看起来应该是这样的:

//UITouch+customInitMethods.h


@interface UITouch (customInitMethods)

- (id)initInView:(UIView *)view;

- (id)initAtPoint:(CGPoint)point inWindow:(UIWindow *)window;

- (id)initAtPoint:(CGPoint)point inView:(UIView *)view;

@end

然后你的实施:

#import "UITouch+customInitMethods.h"

@implementation UITouch (customInitMethod)

//Your method implementations go here.

@end

确保类别文件的.m文件中的目标复选框设置为包含应用程序目标中的类别。

然后,您需要在任何想要使用自定义init方法的文件中#import UITouch + customInitMethods.h。