1
我目前正在尝试编写我的第一个CoreData-Application,它需要访问某些东西的应用程序委托。所以我试图在我的委托中创建一个小变量,我想读取它来确定我是否得到了正确的委托。但是,似乎我无法访问我的委托并创建一个新委托。 这里是我的代码:访问另一个类中的NSApplications委托?
//delegate.h
#import <Cocoa/Cocoa.h>
@interface delegate_TestAppDelegate : NSObject <NSApplicationDelegate> {
@private
NSWindow *window;
NSString * myString;
}
@property (assign) IBOutlet NSWindow *window;
@property (retain) NSString * myString;
@end
//delegate.m
#import "delegate_TestAppDelegate.h"
@implementation delegate_TestAppDelegate
@synthesize window, myString;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.myString = @"Hello, World";
NSLog(@"In delegate: %@", self.myString);
}
@end
//MyClass.h
#import <Foundation/Foundation.h>
#import "delegate_TestAppDelegate.h"
@interface MyClass : NSObject {
@private
delegate_TestAppDelegate * del;
}
- (IBAction)click:(id)sender;
@end
//MyClass.m
#import "MyClass.h"
@implementation MyClass
- (id)init
{
self = [super init];
if (self) {
del = [[NSApplication sharedApplication] delegate];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (IBAction)click:(id)sender {
NSLog(@"Click: %@", del.myString);
}
@end
奇怪的是,该代码返回“在委托:你好,世界”,但“点击:(空)” 哪里是我的错误?
你在哪里初始化MyClass? – thomashw
作为Interface Builder中的NSObject –