如何从实用程序nsobject类中的uiviewcontroller调用方法

时间:2013-03-26 06:18:58

标签: objective-c methods uiviewcontroller import nsobject

如何将我的uiviewcontroller中的方法导入nsobject的实用程序类(我在viewcontroller中使用它来调用常用方法?)我有大量的视图控制器需要调用相同的方法。

我需要使用NSInvocation吗?我应该使用不同的协议和代表吗?

如果没有实用程序类访问该方法,我将获得“NSInvalidArgumentException”

这是我需要使用的基本结构。

utilityClass - .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

1 个答案:

答案 0 :(得分: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

另外,一方注意。建议您使用大写字母启动类名,并使用小写字母启动变量和函数。它有助于使您的代码更具可读性。