Hello everyone
I prefer to access the global instance/variable via delegate. ( I do not like the mode as 'extern NSInteger myInstance')
This means that all global instances/variables are in a delegate object. In cocoa touch, I know I can use
Quote:
appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
UIApplication *app=[UIApplication sharedApplication];
appDelegate.myInstance=1;
|
to access the global instance(myInstance), but I am not sure if it is same as the mode in Cocoa.
So I try to using codes below
Quote:
//the application in MainMenu.XIb set to Ojbect 'AppDelegate'
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>
#import <Cocoa/Cocoa.h>
#import "rootWindowController.h"
@class AppController;
@interface AppDelegate :NSApplication{
IBOutlet AppController *vAppController;
NSInteger NSInteger myInt;;
}
@property (retain,nonatomic)IBOutlet AppController *vAppController;
@property (assign,nonatomic) NSInteger myInt;;
@end
@implementation AppDelegate
@synthesize vAppController;
@synthesize myInt;
- (void)applicationDidFinishLaunching NSNotificatio n *)notification{
}
- (void)dealloc {
[vAppController release];
[super dealloc];
}
#import <Foundation/Foundation.h>
@class AppDelegate;
@interface AppController : NSObject {
AppDelegate *appDelegate;
}
@property (retain,nonatomic) AppDelegate *appDelegate;
@end
//
#import "AppController.h"
@implementation AppController
@synthesize appDelegate;
-(id)init
{
NSApplication *app=[NSApplication sharedApplication];
appDelegate =(AppDelegate *)[app delegate];
[super init];//a: I set the breakpoint here and check the value of appDelegate
appDelegate.nyInt=0;
}
- (void)dealloc {
[appDelegate release];
[super dealloc];
}
@end
|
Then I drag a NSObject to MainMenu.xib and set to type AppController.
I set the AppDelegate outlet delegate to the AppController,
vAppController in AppDelegate links to the AppController also.
I set the breakpoint at A, but I found that
NSApplication *app always work well, but appDelegate always returns 0x0.
This means that no object was initialized for appDelegate.
I stick on the problem for 2,3 days, still no solution.
Welcome any comment
Thanks
interdev