如何将方法从我的 uiviewcontroller 导入到 nsobject 的实用程序类(我在 viewcontroller 中使用它来调用常用方法?)我有大量需要调用相同方法的视图控制器。
我需要使用 NSInvocation 吗?我应该使用不同的东西,比如协议和委托吗?
如果不通过实用程序类访问该方法,我只会得到“NSInvalidArgumentException”
这是我需要使用的基本结构。
实用程序类 - .h
#import <Foundation/Foundation.h>
@interface utilityClass : NSObject
- (void) swipeLoad: (UIView*)myview;
@end
.m
#import "utilityClass.h"
@implementation utilityClass
- (void) swipeLoad: (UIView*) myview
{
UISwipeGestureRecognizer *oneFingerSwipeRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[myview addGestureRecognizer:oneFingerSwipeRight];
UISwipeGestureRecognizer *oneFingerSwipeLeft =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[myview addGestureRecognizer:oneFingerSwipeLeft];
UISwipeGestureRecognizer *twoFingerSwipeRight =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerSwiperight:)];
[twoFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
twoFingerSwipeRight.numberOfTouchesRequired = 2;
[myview addGestureRecognizer:twoFingerSwipeRight];
UISwipeGestureRecognizer *twoFingerSwipeLeft =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerSwipeLeft:)];
[twoFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
twoFingerSwipeLeft.numberOfTouchesRequired = 2;
[myview addGestureRecognizer:twoFingerSwipeLeft];
}
@end
视图控制器 - .h
#import <UIKit/UIKit.h>
#import "utilityClass.h"
@class utilityClass;
@interface viewController : UIViewController
{
utilityClass *utilityClass;
}
@property (nonatomic, retain) utilityClass*utilityClass;
@end
.m
#import "viewcontroller.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
@synthesize utilityClass = _utilityClass;
- (void)viewDidLoad
{
[super viewDidLoad];
self.utilityClass = [[utilityClass alloc] init];
[self.utilityClass swipeLoad:self.view];
}
- (void)twoFingerSwiperight:(UISwipeGestureRecognizer *)recognizer {
//[self another_method]
}
- (void)twoFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
//[self another_method]
}
- (void)oneFingerSwipeRight:(UISwipeGestureRecognizer *)recognizer {
//[self another_method]
}
- (void)oneFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
//[self another_method]
}
@end