我已经通过以下方式成功地设置了对另一种方法的访问:
SomeScript.m(我试图访问的类)
-(void)amethod{....
{} 中的 codeA.h(正在访问方法的类)具有:
SomeScript* myScript;
代码A.m
myScript = [[SomeScript alloc] init];
[myScript amethod];
但是,我想要做的是将其作为应用程序委托(delegate)的实例变量,但是当我将 SomeScript* myScript;和 myScript = [[SomeScript alloc] init];在 AppDelegate.h 中,codeA.m 无法识别它。
最佳答案
首先,向 AppDelegate
添加属性 myScript
:
在 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];
关于objective-c - Xcode : Accessing method instance from other class file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9434432/