在另一个类中访问NSApplications委派? [英] Accessing an NSApplications delegate in another class?
问题描述
我目前正在尝试写我的第一个CoreData应用程序,需要访问的应用程序委派的一些东西。所以我试图在我的委托,我想读一个小变量,以确定我是否获得正确的代理。但是,似乎我无法访问我的代理,并创建一个新的。
这是我的代码:
// 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
#importdelegate_TestAppDelegate.h
@implementation delegate_TestAppDelegate
@synthesize window,myString;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.myString = @Hello,World;
NSLog(@在委托:%@,self.myString);
}
@end
//MyClass.h
#import< Foundation / Foundation.h>
#importdelegate_TestAppDelegate.h
@interface MyClass:NSObject {
@private
delegate_TestAppDelegate * del;
}
- (IBAction)click:(id)sender;
@end
//MyClass.m
#importMyClass.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);奇怪的是,这段代码返回In delegate:,然后单击下一步,然后单击下一步 Hello,World,但点击:(null)
我的错误在哪里?解决方案在任何分配给应用程序的委托
属性之前,重新分配 del
。我建议你摆脱 del
指针,只需调用 [[NSApplication sharedApplication] delegate]
时间你需要委托。
I'm currently trying to write my first CoreData-Application, which needs to access the applications delegate for some stuff. So I was trying to make a little variable inside my delegate which I wanted to read to determine if I got the correct delegate. However, it seems like I'm unable to access my delegate and create a new one instead.
Here is my code:
//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
Strangely enough, this code returns "In delegate: Hello, World", but "Click: (null)"
Where is my error?
解决方案 I suspect you're assigning del
before anything has been assigned to the delegate
property of the application. I'd recommend you get rid of the del
pointer altogether, and simply call [[NSApplication sharedApplication] delegate]
each time you need the delegate.
这篇关于在另一个类中访问NSApplications委派?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!