【问题标题】:crash when using a category使用类别时崩溃
【发布时间】:2014-11-03 14:51:23
【问题描述】:

我在这段代码中创建了一个 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:]: unrecognized selector sent to instance 我该如何解决?

【问题讨论】:

  • 为什么投反对票??

标签: ios objective-c uitouch


【解决方案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 文件上的目标复选框设置为在您的应用程序目标中包含该类别。

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

【讨论】:

  • 你拯救了我的一天,我的类别的目标没有设置:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 2012-02-20
  • 2020-12-18
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多