0

如何将方法从我的 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
4

1 回答 1

1

听起来你最好继承 UIViewController 并将你的滑动方法放在那里。然后让所有视图控制器成为新子类的子类(在下面的示例中,您的视图控制器将是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 回答