1
我創建了一個插件,並且有一些名爲PluginPrincipalClass,ClassOne和ClassTwo的3個類。在我的類中有以下代碼片段。如何調用插件主體類以外的類的方法
#import <Cocoa/Cocoa.h>
@interface PluginPrincipalClass : NSObject
@end
#import "PluginPrincipalClass.h"
@implementation PluginPrincipalClass
- (NSInteger)getDownloadPercentage
{
NSLog(@"getDownloadPercentage");
return 10;
}
- (void)downloadSoftwareUpdate
{
NSLog(@"downloadSoftwareUpdate");
}
@end
#import <Cocoa/Cocoa.h>
@interface ClassOne : NSObject
@end
#import "ClassOneh"
@implementation ClassOne
- (void)ClassOneMethod
{
NSLog(@"ClassOneMethod");
}
@end
#import <Cocoa/Cocoa.h>
@interface ClassTwo : NSObject
@end
#import "ClassTwo.h"
@implementation ClassTwo
- (void)ClassTwoMethod
{
NSLog(@"ClassTwoMethod");
}
@end
在我BaseApplication加載插件,並呼籲主要類我有下面的代碼片段
NSString *zStrPlugInsPath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *zAryBundlePaths = [NSBundle pathsForResourcesOfType:@"plugin"
inDirectory:zStrPlugInsPath];
NSString * zStrPathToPlugin = [zAryBundlePaths lastObject];
NSBundle *znsBundlePlugin = [NSBundle bundleWithPath:zStrPathToPlugin];
// instantiate the principal class and call the method
Class zPrincipalClass = [znsBundlePlugin principalClass];
id zPrincipalClassObj = [[zPrincipalClass alloc]init];
NSInteger downloadPer = [zPrincipalClassObj getDownloadPercentage];
NSLog(@"downloadPer = %ld",downloadPer);
[zPrincipalClassObj downloadSoftwareUpdate];
這是工作fine.If我想打電話給ClassOne或ClassTwo的方法。如何從我的基本應用程序實例化和調用這些方法。是否類似於創建對象ClassOne並使用該對象調用方法?
感謝您的答覆,現在我不能對此進行測試代碼因爲我沒有Mac,所以你的意思是說我可以用代碼調用ClassOne方法,Class zSecondaryClass = [znsBundlePlugin classNamed:@「ClassOne」]; id zSecondaryClassObj = [[zSecondaryClassObj alloc] init]; [zSecondaryClassObj ClassOneMethod];對? – Akbar 2012-04-23 16:58:32
我相信如此(假設我正確地遵循你的命名/邏輯) – RyanWilcox 2012-04-23 17:00:56