Xcode:从其他类文件访问方法实例

时间:2012-02-24 16:32:56

标签: objective-c xcode methods

我通过以下方式合理地成功设置了对其他方法的访问权限:

SomeScript.m(我正在尝试访问的类)

-(void)amethod{....
{}中的codeA.h(正在访问amethod的类)具有:

SomeScript* myScript;

codeA.m

myScript = [[SomeScript alloc] init];
[myScript amethod];

我想要做的是将它作为app委托的实例变量,但是当我放入SomeScript * myScript时;和myScript = [[SomeScript alloc] init];在AppDelegate.h中,codeA.m无法识别它。

1 个答案:

答案 0 :(得分:2)

首先,将属性myScript添加到AppDelegate

在AppDelegate.h中

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) SomeScript *myScript; // Add this line

//...

@end

在AppDelegate.m中

@implementation PCAppDelegate

@synthesize myScript; // Add this line

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.myScript = [[SomeScript alloc] init]; // Add this line

//...

@end

现在您已声明并初始化了该属性,您可以像使用其他类中的以下内容一样使用它:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.myScript aMethod];