View Full Version : Accessing the app delegate

Dec 17, 2008, 10:55 AM
This is driving me potty and I just can't figure out where I am going wrong!

Lets say I have an app delegate and three classes:

AppDelegate, Class1, Class2 and Class3

In AppDelegate I have an NSArray instance variable called ....Barry (lol)
I also have in AppDelegate an NSUinteger outside of the interface block to hold the value of an indexPath from a table call.

In Class1 I can import the app delegate .h file, create a reference to Barry using this reference and read and write its values.

When I step through the code in debug I can see that instanceOfAppDelegate.Barry has been set and contains the one object Class1 writes to it.

But if I then try the same thing in Class3 which is different from Class1 but needs to access Barry I do not get any errors but when querying instanceOfAppDelegate.Barry in Class3 it remains unset as it I dont think it is pointing to Barry in AppDelegate


Dec 17, 2008, 11:53 AM
Maybe some code snippets will help us understand what you're doing.

Dec 17, 2008, 01:05 PM
good point!


#import <UIKit/UIKit.h>

NSUInteger rowIndex;

@interface AppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
UINavigationController *navigationController;

NSArray *Barry;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (retain, nonatomic) NSArray *Barry;



#import "AppDelegate.h"
#import "Class1.h"

@implementation AppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize Barry;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];

- (void)dealloc {
[navigationController release];
[window release];
[Barry release];
[super dealloc];



#import <UIKit/UIKit.h>

@class AppDelegate;

@interface Class1 : UITableViewController <UITableViewDelegate, UITableViewDataSource>
AppDelegate *myAppDelegate;

@property (retain, nonatomic) AppDelegate *myAppDelegate;



myAppDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

rowIndex = [indexPath row];

NSDictionary *rowData = [myAppDelegate.Barry objectAtIndex: rowIndex];


And here is the result of the German panel....beardedpig nil point!

I had not placed this line in Class3

myAppDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

So by doing this little exercise I found my doofish error!!!!!

Thanks ;-)))

Dec 17, 2008, 01:23 PM
So by doing this little exercise I found my doofish error!!!!!

Thanks ;-)))
Glad I could help? :confused: