我目前正在尝试编写我的第一个CoreData-Application,它需要访问应用程序委托以获取某些内容.所以我试图在我的委托中创建一个小变量,我想阅读以确定我是否得到了正确的委托.但是,似乎我无法访问我的代理并创建一个新的代理.这是我的代码:
//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
Run Code Online (Sandbox Code Playgroud)
奇怪的是,这段代码返回"In delegate:Hello,World",但是"Click:(null)"我的错误在哪里?
我怀疑你在将del任何东西分配给delegate应用程序的属性之前进行分配.我建议你del完全摆脱指针,只需在[[NSApplication sharedApplication] delegate]每次需要委托时调用.
| 归档时间: |
|
| 查看次数: |
2277 次 |
| 最近记录: |