I'm developing an app for OS X and I got stuck with a problem.
The structure of the app is :
AppDelegate: where the main actions take place from MainMenu.xib. It's also where the main window is created.
SettingsWindowController: where a settings window is defined with interface from SettingsWindow.xib.
I created a protocol SettingsWindowProtocol to delegate those two classes.
I created one more class dbHandler which is initialized as an obj
dbh
in AppDelegate.In SettingsWindowProtocol I created a method to send the object but after I call this method in SettingsWindowControl the object is null.
The question is how I share in two different classes the same object ?
A few blocks of code related to question:
AppDelegate.h:
#import "dbHandler.h"
...
@property (retain) dbHandler *dbh;
@interface AppDelegate : NSObject <SettingsWindowControllerProtocol>
-(dbHandler*)sendDBobject;
AppDelegate.m:
...
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
dbh = [[dbHandler alloc]init];
...
}
- (IBAction)settingsButton:(id)sender {
if(!settingsWindowController){
settingsWindowController = [[SettingsWindowController alloc] initWithWindowNibName:@"SettingsWindow"];
}
[settingsWindowController showWindow:self];
settingsWindowController.delegate = self;
}
-(dbHandler*)sendDBobject{
return dbh;
}
SettingsWindowController.h :
#import "dbHandler.h"
@protocol SettingsWindowControllerProtocol<NSObject>
-(dbHandler*)sendDBobject;
@end
@interface SettingsWindowController : NSWindowController
@property (assign) id<SettingsWindowControllerProtocol> delegate;
@property (retain) dbHandler* dbh;
@end
SettingsWindowController.m:
- (void)windowDidLoad
{
dbh = [delegate sendDBobject];
[super windowDidLoad];
NSLog(@"settings string returned: %@",[dbh returnString]);
}
See Question&Answers more detail:os