0

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
4

1 に答える 1

1

UIViewControllerをサブクラス化し、そこにスワイプメソッドを配置する方がよいようです。次に、すべてのView Controllerを新しいサブクラスのサブクラスにします(以下の例では、ViewControllerはのサブクラスになりますMyViewController

次に、ユーティリティクラスを完全に削除します。

// MyViewController.h

#import <UIKit/UIKit.h>

@implementation MyViewController : UIViewController

@end

// MyViewController.m

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UISwipeGestureRecognizer *oneFingerSwipeRight =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeRight:)];
    [oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:oneFingerSwipeRight];

    UISwipeGestureRecognizer *oneFingerSwipeLeft =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeLeft:)];
    [oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:oneFingerSwipeLeft];

    UISwipeGestureRecognizer *twoFingerSwipeRight =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerSwiperight:)];
    [twoFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    twoFingerSwipeRight.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:twoFingerSwipeRight];

    UISwipeGestureRecognizer *twoFingerSwipeLeft =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerSwipeLeft:)];
    [twoFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    twoFingerSwipeLeft.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:twoFingerSwipeLeft];
}

- (void)oneFingerSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    // Your code here
}

- (void)oneFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    // Your code here
}

- (void)twoFingerSwiperight:(UISwipeGestureRecognizer *)recognizer {
    // Your code here
}

- (void)twoFingerSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    // Your code here
}

@end

また、片側のメモ。クラス名は大文字で開始し、変数と関数は小文字で開始することをお勧めします。コードをより読みやすくするのに役立ちます。

于 2013-03-26T06:57:14.393 に答える