2014-11-03 74 views
-1

當我創建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:]:無法識別的選擇發送到實例 我該如何解決這個問題?

+0

爲什麼投降? – HDNZ 2014-11-04 07:55:30

回答

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。

+0

ü保存我的一天我的類別的目標沒有設置:) – HDNZ 2014-11-03 15:05:26

+0

很高興我能幫上忙。我已經做了不止一次... – 2014-11-03 17:55:47