Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-19-2011, 03:52 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 12
interdev is on a distinguished road
Default how to access global instance/variable via delegate?

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)applicationDidFinishLaunchingNSNotificatio 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
interdev is offline   Reply With Quote
Old 01-20-2011, 07:01 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1,018
Tambourin is on a distinguished road
Default

Why not just put your variables in NSApplication
Tambourin is offline   Reply With Quote
Reply

Bookmarks

Tags
cocoa

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 374
8 members and 366 guests
husthlj, illogical, LegionMD, LunarMoon, mer10, Murphy, padsoftware, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0