uiviewcontrollerからnsobjectのユーティリティクラス(一般的なメソッドを呼び出すためにviewcontrollerで使用します)にメソッドをインポートするにはどうすればよいですか?同じメソッドを呼び出す必要のある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